2019-02-04 23:52:21 +00:00
|
|
|
|
// <copyright file="IntelProcessor.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace EightBit
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
public abstract class IntelProcessor : LittleEndianProcessor
|
|
|
|
|
{
|
2019-02-03 20:29:52 +00:00
|
|
|
|
private readonly IntelOpCodeDecoded[] decodedOpCodes = new IntelOpCodeDecoded[0x100];
|
2019-02-03 00:42:55 +00:00
|
|
|
|
private ushort sp;
|
|
|
|
|
private ushort memptr;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
private PinLevel haltLine;
|
|
|
|
|
|
|
|
|
|
protected IntelProcessor(Bus bus)
|
|
|
|
|
: base(bus)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.sp = (ushort)Mask.Mask16;
|
|
|
|
|
this.memptr = (ushort)Mask.Mask16;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 0x100; ++i)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
|
|
|
|
this.decodedOpCodes[i] = new IntelOpCodeDecoded((byte)i);
|
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> RaisingHALT;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public event EventHandler<EventArgs> RaisedHALT;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public event EventHandler<EventArgs> LoweringHALT;
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public event EventHandler<EventArgs> LoweredHALT;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public ushort SP { get => this.sp; set => this.sp = value; }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public ushort MEMPTR { get => this.memptr; set => this.memptr = value; }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public abstract ushort AF { get; set; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
|
|
|
|
public byte A { get => Chip.HighByte(this.AF); set => this.AF = (ushort)(Chip.LowerPart(this.AF) | Chip.PromoteByte(value)); }
|
|
|
|
|
|
|
|
|
|
public byte F { get => Chip.LowByte(this.AF); set => this.AF = (ushort)(Chip.HigherPart(this.AF) | value); }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public abstract ushort BC { get; set; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
|
|
|
|
public byte B { get => Chip.HighByte(this.BC); set => this.BC = (ushort)(Chip.LowerPart(this.BC) | Chip.PromoteByte(value)); }
|
|
|
|
|
|
|
|
|
|
public byte C { get => Chip.LowByte(this.BC); set => this.BC = (ushort)(Chip.HigherPart(this.BC) | value); }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public abstract ushort DE { get; set; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
|
|
|
|
public byte D { get => Chip.HighByte(this.DE); set => this.DE = (ushort)(Chip.LowerPart(this.DE) | Chip.PromoteByte(value)); }
|
|
|
|
|
|
|
|
|
|
public byte E { get => Chip.LowByte(this.DE); set => this.DE = (ushort)(Chip.HigherPart(this.DE) | value); }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public abstract ushort HL { get; set; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
|
|
|
|
public byte H { get => Chip.HighByte(this.HL); set => this.HL = (ushort)(Chip.LowerPart(this.HL) | Chip.PromoteByte(value)); }
|
|
|
|
|
|
|
|
|
|
public byte L { get => Chip.LowByte(this.HL); set => this.HL = (ushort)(Chip.HigherPart(this.HL) | value); }
|
|
|
|
|
|
|
|
|
|
protected bool Halted => this.HALT().Lowered();
|
|
|
|
|
|
|
|
|
|
public ref PinLevel HALT() => ref this.haltLine;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public override void RaisePOWER()
|
|
|
|
|
{
|
|
|
|
|
base.RaisePOWER();
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.RaiseHALT();
|
|
|
|
|
this.SP = this.AF = this.BC = this.DE = this.HL = (ushort)Mask.Mask16;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void RaiseHALT()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.OnRaisingHALT();
|
|
|
|
|
this.HALT().Raise();
|
|
|
|
|
this.OnRaisedHALT();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void LowerHALT()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.OnLoweringHALT();
|
|
|
|
|
this.HALT().Lower();
|
|
|
|
|
this.OnLoweredHALT();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
protected virtual void OnRaisingHALT() => this.RaisingHALT?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
|
|
protected virtual void OnRaisedHALT() => this.RaisedHALT?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
|
|
protected virtual void OnLoweringHALT() => this.LoweringHALT?.Invoke(this, EventArgs.Empty);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
protected virtual void OnLoweredHALT() => this.LoweredHALT?.Invoke(this, EventArgs.Empty);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
protected override void HandleRESET()
|
|
|
|
|
{
|
|
|
|
|
base.HandleRESET();
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.PC = 0;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
protected sealed override void Push(byte value) => this.Bus.Write(--this.SP, value);
|
|
|
|
|
|
|
|
|
|
protected sealed override byte Pop() => this.Bus.Read(this.SP++);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
protected sealed override ushort GetWord()
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
var returned = base.GetWord();
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = this.Bus.Address;
|
|
|
|
|
return returned;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
protected sealed override void SetWord(ushort value)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
base.SetWord(value);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = this.Bus.Address;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
////
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
protected void Restart(byte address)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = address;
|
|
|
|
|
this.Call(this.MEMPTR);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool CallConditional(bool condition)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = this.FetchWord();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
if (condition)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
|
|
|
|
this.Call(this.MEMPTR);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
return condition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool JumpConditional(bool condition)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = this.FetchWord();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
if (condition)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
|
|
|
|
this.Jump(this.MEMPTR);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
return condition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool ReturnConditional(bool condition)
|
|
|
|
|
{
|
|
|
|
|
if (condition)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
|
|
|
|
this.Return();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
return condition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void JumpRelative(sbyte offset)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = (ushort)(this.PC + offset);
|
|
|
|
|
this.Jump(this.MEMPTR);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool JumpRelativeConditional(bool condition)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
var offset = (sbyte)this.FetchByte();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
if (condition)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
|
|
|
|
this.JumpRelative(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
return condition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override sealed void Return()
|
|
|
|
|
{
|
|
|
|
|
base.Return();
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.MEMPTR = this.PC;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Halt()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
--this.PC;
|
|
|
|
|
this.LowerHALT();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Proceed()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
++this.PC;
|
|
|
|
|
this.RaiseHALT();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|