diff --git a/Z80/Z80.Test/Board.cs b/Z80/Z80.Test/Board.cs index 4263854..34c183e 100644 --- a/Z80/Z80.Test/Board.cs +++ b/Z80/Z80.Test/Board.cs @@ -1,12 +1,115 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// +// Copyright (c) Adrian Conlon. All rights reserved. +// namespace Z80.Test { - class Board + using System.Text; + using EightBit; + + internal class Board : Bus { + private readonly Configuration configuration; + private readonly Ram ram; + private readonly InputOutput ports; + private readonly Disassembler disassembler; + private readonly MemoryMapping mapping; + + private int warmstartCount = 0; + + public Board(Configuration configuration) + { + this.configuration = configuration; + this.ram = new Ram(0x10000); + this.ports = new InputOutput(); + this.CPU = new Z80(this, this.ports); + this.disassembler = new Disassembler(this); + this.mapping = new MemoryMapping(this.ram, 0x0000, (ushort)Mask.Mask16, AccessLevel.ReadWrite); + } + + public Z80 CPU { get; } + + public override void RaisePOWER() + { + base.RaisePOWER(); + this.CPU.RaisePOWER(); + this.CPU.RaiseRESET(); + this.CPU.RaiseINT(); + this.CPU.RaiseNMI(); + } + + public override void LowerPOWER() + { + this.CPU.LowerPOWER(); + base.LowerPOWER(); + } + + public override void Initialize() + { + var programFilename = this.configuration.Program; + var programPath = this.configuration.RomDirectory + "/" + this.configuration.Program; + var loadAddress = this.configuration.LoadAddress; + this.ram.Load(programPath, loadAddress.Word); + + this.CPU.LoweredHALT += this.CPU_LoweredHALT; + this.CPU.ExecutingInstruction += this.CPU_ExecutingInstruction_CPM; + + if (this.configuration.DebugMode) + { + this.CPU.ExecutingInstruction += this.CPU_ExecutingInstruction_Debug; + } + + this.Poke(0, 0xc3); // JMP + this.CPU.PokeWord(1, this.configuration.StartAddress); + this.Poke(5, 0xc9); // ret + } + + public override MemoryMapping Mapping(ushort absolute) => this.mapping; + + private void BDOS() + { + switch (CPU.C()) + { + case 0x2: + System.Console.Out.Write(CPU.E().ToString()); + break; + case 0x9: + for (ushort i = CPU.DE().Word; Peek(i) != '$'; ++i) + { + System.Console.Out.Write((char)Peek(i)); + } + + break; + } + } + + private void CPU_ExecutingInstruction_CPM(object sender, System.EventArgs e) + { + switch (this.CPU.PC().Word) + { + case 0x0: // CP/M warm start + if (++this.warmstartCount == 2) + { + this.LowerPOWER(); + } + + break; + case 0x5: // BDOS + BDOS(); + break; + default: + break; + } + } + + private void CPU_LoweredHALT(object sender, System.EventArgs e) + { + this.LowerPOWER(); + } + + private void CPU_ExecutingInstruction_Debug(object sender, System.EventArgs e) + { + System.Console.Error.WriteLine($"{EightBit.Disassembler.State(this.CPU)}\t{this.disassembler.Disassemble(this.CPU)}"); + } } } diff --git a/Z80/Z80.Test/Configuration.cs b/Z80/Z80.Test/Configuration.cs index 9fdccce..fa22c9d 100644 --- a/Z80/Z80.Test/Configuration.cs +++ b/Z80/Z80.Test/Configuration.cs @@ -1,12 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// +// Copyright (c) Adrian Conlon. All rights reserved. +// namespace Z80.Test { - class Configuration + using EightBit; + + internal class Configuration { + public Configuration() + { + } + + public bool DebugMode { get; set; } = false; + + public Register16 LoadAddress { get; } = new Register16(0x100); + + public Register16 StartAddress { get; } = new Register16(0x100); + + public string RomDirectory { get; } = "roms"; + + public string Program { get; } = "zexall.com"; } -} +} \ No newline at end of file diff --git a/Z80/Z80.Test/Program.cs b/Z80/Z80.Test/Program.cs index cc85882..9670a44 100644 --- a/Z80/Z80.Test/Program.cs +++ b/Z80/Z80.Test/Program.cs @@ -1,12 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// +// Copyright (c) Adrian Conlon. All rights reserved. +// namespace Z80.Test { - class Program + public static class Program { + public static void Main(string[] args) + { + var configuration = new Configuration(); + +#if DEBUG + configuration.DebugMode = true; +#endif + + using (var harness = new TestHarness(configuration)) + { + harness.Run(); + } + } } } diff --git a/Z80/Z80.Test/TestHarness.cs b/Z80/Z80.Test/TestHarness.cs index 8d7d2db..27e73da 100644 --- a/Z80/Z80.Test/TestHarness.cs +++ b/Z80/Z80.Test/TestHarness.cs @@ -1,12 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// +// Copyright (c) Adrian Conlon. All rights reserved. +// namespace Z80.Test { - class TestHarness + using System; + using System.Diagnostics; + + internal class TestHarness : IDisposable { + private readonly Stopwatch timer = new Stopwatch(); + private readonly Board board; + private long totalCycles = 0; + private long instructions = 0; + + private bool disposed = false; + + public TestHarness(Configuration configuration) + { + this.board = new Board(configuration); + } + + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + public void Run() + { + this.board.Initialize(); + this.board.RaisePOWER(); + + var cpu = this.board.CPU; + + this.timer.Start(); + while (cpu.Powered) + { + this.totalCycles += cpu.Step(); + ++this.instructions; + } + + this.timer.Stop(); + } + + protected virtual void Dispose(bool disposing) + { + if (!this.disposed) + { + if (disposing) + { + System.Console.Out.WriteLine($"Guest cycles = {this.totalCycles}"); + System.Console.Out.WriteLine($"Seconds = {this.timer.ElapsedMilliseconds / 1000.0}"); + } + + this.disposed = true; + } + } } } diff --git a/Z80/Z80.Test/Z80.Test.csproj b/Z80/Z80.Test/Z80.Test.csproj index 5074f2d..4a1b90c 100644 --- a/Z80/Z80.Test/Z80.Test.csproj +++ b/Z80/Z80.Test/Z80.Test.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {F749BEAE-8903-400B-875C-1220ADCFEF08} - Library + Exe Properties Z80.Test Z80.Test @@ -18,9 +18,12 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG prompt 4 + true + AllRules.ruleset + latest pdbonly @@ -30,6 +33,10 @@ prompt 4 true + latest + + + @@ -50,6 +57,17 @@ + + + + + {6ebf8857-62a3-4ef4-af21-c1844031d7e4} + EightBit + + + {c00648c1-bac1-4efb-816f-e87c091619d7} + Z80 + \ No newline at end of file diff --git a/Z80/Z80.Test/stylecop.json b/Z80/Z80.Test/stylecop.json new file mode 100644 index 0000000..3af08b4 --- /dev/null +++ b/Z80/Z80.Test/stylecop.json @@ -0,0 +1,19 @@ +{ + // ACTION REQUIRED: This file was automatically added to your project, but it + // will not take effect until additional steps are taken to enable it. See the + // following page for additional information: + // + // https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md + + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "documentationRules": { + "documentInterfaces": false, + "documentExposedElements": false, + "documentInternalElements": false, + "documentPrivateElements": false, + "documentPrivateFields": false, + "companyName": "Adrian Conlon" + } + } +}