Add Z80 processor (untested, but complete)

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2019-02-16 21:32:34 +00:00
parent 3108a373d7
commit ea82c58777
11 changed files with 2213 additions and 14 deletions

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EightBit.UnitTest", "EightB
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M6502.Test", "M6502\M6502.Test\M6502.Test.csproj", "{854EDE2F-B54D-425C-8F68-EDA68BDC797E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M6502.Test", "M6502\M6502.Test\M6502.Test.csproj", "{854EDE2F-B54D-425C-8F68-EDA68BDC797E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z80", "Z80\Z80.csproj", "{C00648C1-BAC1-4EFB-816F-E87C091619D7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -69,6 +71,18 @@ Global
{854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x64.Build.0 = Release|Any CPU {854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x64.Build.0 = Release|Any CPU
{854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x86.ActiveCfg = Release|Any CPU {854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x86.ActiveCfg = Release|Any CPU
{854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x86.Build.0 = Release|Any CPU {854EDE2F-B54D-425C-8F68-EDA68BDC797E}.Release|x86.Build.0 = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|x64.ActiveCfg = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|x64.Build.0 = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|x86.ActiveCfg = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Debug|x86.Build.0 = Debug|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|Any CPU.Build.0 = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|x64.ActiveCfg = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|x64.Build.0 = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|x86.ActiveCfg = Release|Any CPU
{C00648C1-BAC1-4EFB-816F-E87C091619D7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -51,5 +51,23 @@ namespace EightBit
public static int PromoteNibble(byte value) => LowByte(value << 4); public static int PromoteNibble(byte value) => LowByte(value << 4);
public static int DemoteNibble(byte value) => HighNibble(value); public static int DemoteNibble(byte value) => HighNibble(value);
public static int CountBits(int value)
{
int count = 0;
while (value != 0)
{
++count;
value &= value - 1;
}
return count;
}
public static int CountBits(byte value) => CountBits((int)value);
public static bool EvenParity(int value) => CountBits(value) % 2 == 0;
public static bool EvenParity(byte value) => EvenParity((int)value);
} }
} }

View File

@ -6,23 +6,27 @@ namespace EightBit
{ {
public class IntelOpCodeDecoded public class IntelOpCodeDecoded
{ {
private readonly int x;
private readonly int y;
private readonly int z;
private readonly int p;
private readonly int q;
public IntelOpCodeDecoded() public IntelOpCodeDecoded()
{ {
} }
public IntelOpCodeDecoded(byte opCode) public IntelOpCodeDecoded(byte opCode)
{ {
this.x = (opCode & 0b11000000) >> 6; // 0 - 3 this.X = (opCode & 0b11000000) >> 6; // 0 - 3
this.y = (opCode & 0b00111000) >> 3; // 0 - 7 this.Y = (opCode & 0b00111000) >> 3; // 0 - 7
this.z = opCode & 0b00000111; // 0 - 7 this.Z = opCode & 0b00000111; // 0 - 7
this.p = (this.y & 0b110) >> 1; // 0 - 3 this.P = (this.Y & 0b110) >> 1; // 0 - 3
this.q = this.y & 1; // 0 - 1 this.Q = this.Y & 1; // 0 - 1
} }
public int X { get; }
public int Y { get; }
public int Z { get; }
public int P { get; }
public int Q { get; }
} }
} }

View File

@ -8,6 +8,9 @@ namespace EightBit
public abstract class IntelProcessor : LittleEndianProcessor public abstract class IntelProcessor : LittleEndianProcessor
{ {
private static readonly int[] HalfCarryTableAdd = new int[8] { 0, 0, 1, 0, 1, 0, 1, 1 };
private static readonly int[] HalfCarryTableSub = new int[8] { 0, 1, 1, 1, 0, 0, 0, 1 };
private readonly IntelOpCodeDecoded[] decodedOpCodes = new IntelOpCodeDecoded[0x100]; private readonly IntelOpCodeDecoded[] decodedOpCodes = new IntelOpCodeDecoded[0x100];
private Register16 sp = new Register16((ushort)Mask.Mask16); private Register16 sp = new Register16((ushort)Mask.Mask16);
@ -86,6 +89,23 @@ namespace EightBit
this.OnLoweredHALT(); this.OnLoweredHALT();
} }
protected static int BuildHalfCarryIndex(byte before, byte value, int calculation)
{
return ((before & 0x88) >> 1) | ((value & 0x88) >> 2) | ((calculation & 0x88) >> 3);
}
protected static int CalculateHalfCarryAdd(byte before, byte value, int calculation)
{
var index = BuildHalfCarryIndex(before, value, calculation);
return HalfCarryTableAdd[index & (int)Mask.Mask3];
}
protected static int CalculateHalfCarrySub(byte before, byte value, int calculation)
{
var index = BuildHalfCarryIndex(before, value, calculation);
return HalfCarryTableSub[index & (int)Mask.Mask3];
}
protected virtual void OnRaisingHALT() => this.RaisingHALT?.Invoke(this, EventArgs.Empty); protected virtual void OnRaisingHALT() => this.RaisingHALT?.Invoke(this, EventArgs.Empty);
protected virtual void OnRaisedHALT() => this.RaisedHALT?.Invoke(this, EventArgs.Empty); protected virtual void OnRaisedHALT() => this.RaisedHALT?.Invoke(this, EventArgs.Empty);
@ -192,5 +212,7 @@ namespace EightBit
++this.PC(); ++this.PC();
this.RaiseHALT(); this.RaiseHALT();
} }
protected IntelOpCodeDecoded GetDecodedOpCode(byte opCode) => this.decodedOpCodes[opCode];
} }
} }

View File

@ -4,8 +4,6 @@
namespace EightBit namespace EightBit
{ {
using System;
public enum Mask public enum Mask
{ {
None = 0, None = 0,

View File

@ -76,7 +76,7 @@ namespace EightBit
return false; return false;
} }
Register16 rhs = (Register16)obj; var rhs = (Register16)obj;
return rhs.Word == this.Word; return rhs.Word == this.Word;
} }

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Z80")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Z80")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c00648c1-bac1-4efb-816f-e87c091619d7")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

59
Z80/RefreshRegister.cs Normal file
View File

@ -0,0 +1,59 @@
namespace EightBit
{
public struct RefreshRegister
{
private readonly byte high;
private byte variable;
public RefreshRegister(byte value)
{
this.high = (byte)(value & (byte)Bits.Bit7);
this.variable = (byte)(value & (byte)Mask.Mask7);
}
public static implicit operator byte(RefreshRegister input)
{
return ToByte(input);
}
public static implicit operator RefreshRegister(byte input)
{
return FromByte(input);
}
public static byte ToByte(RefreshRegister input)
{
return (byte)((input.high << 7) | input.variable);
}
public static RefreshRegister FromByte(byte input)
{
return new RefreshRegister(input);
}
public static RefreshRegister operator ++(RefreshRegister value) => Increment(value);
public static bool operator ==(RefreshRegister left, RefreshRegister right) => left.Equals(right);
public static bool operator !=(RefreshRegister left, RefreshRegister right) => !(left == right);
public static RefreshRegister Increment(RefreshRegister value)
{
++value.variable;
return value;
}
public override bool Equals(object obj)
{
if (!(obj is RefreshRegister))
{
return false;
}
var rhs = (RefreshRegister)obj;
return rhs.high == this.high && rhs.variable == this.variable;
}
public override int GetHashCode() => this.high + this.variable;
}
}

39
Z80/StatusBits.cs Normal file
View File

@ -0,0 +1,39 @@
// <copyright file="StatusBits.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace EightBit
{
using System;
[Flags]
public enum StatusBits
{
// Negative
SF = Bits.Bit7,
// Zero
ZF = Bits.Bit6,
// Undocumented Y flag
YF = Bits.Bit5,
// Half carry
HC = Bits.Bit4,
// Undocumented X flag
XF = Bits.Bit3,
// Parity
PF = Bits.Bit2,
// Zero
VF = Bits.Bit2,
// Negative?
NF = Bits.Bit1,
// Carry
CF = Bits.Bit0,
}
}

1951
Z80/Z80.cs Normal file

File diff suppressed because it is too large Load Diff

58
Z80/Z80.csproj Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C00648C1-BAC1-4EFB-816F-E87C091619D7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Z80</RootNamespace>
<AssemblyName>Z80</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="RefreshRegister.cs" />
<Compile Include="StatusBits.cs" />
<Compile Include="Z80.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EightBit\EightBit.csproj">
<Project>{6ebf8857-62a3-4ef4-af21-c1844031d7e4}</Project>
<Name>EightBit</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>