Skip to content

Commit

Permalink
Cleaup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mch2112 committed Apr 10, 2017
1 parent b511bf9 commit 4291ffa
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 123 deletions.
17 changes: 5 additions & 12 deletions Z80/Assembler.Macro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public Macro(string line)
for (int i = 0; i < arguments.Count; i++)
Debug.Assert(arguments[i] == arguments[i].ToUpper());
}
public void AddLine(string Line)
{
this.lines.Add(Line);
}
public void AddLine(string Line) => lines.Add(Line);
public List<string> Expand(string inputArguments, int InputLineNumber, out string Error)
{
Error = String.Empty;
Expand All @@ -39,22 +36,18 @@ public List<string> Expand(string inputArguments, int InputLineNumber, out strin
string[] inputArgs = GetCSV(inputArguments, 1000);

if (inputArgs.Length != arguments.Count)
Error = string.Format("Macro {0} Arguments Mismatch: {1} Required, {2} Specified, Line {3}",
Name,
arguments.Count,
inputArgs.Length,
InputLineNumber);
Error = string.Format($"Macro {Name} Arguments Mismatch: {arguments.Count} Required, {inputArgs.Length} Specified, Line {InputLineNumber}");

foreach (string l in lines)
{
argNum = 0;
line = l;
foreach (string a in arguments)
foreach (string arg in arguments)
{
if (argNum < inputArgs.Length)
line = line.Replace("&" + a, inputArgs[argNum++]);
line = line.Replace("&" + arg, inputArgs[argNum++]);
else
line = line.Replace("&" + a, String.Empty);
line = line.Replace("&" + arg, String.Empty);
}
returnLines.Add(line);
}
Expand Down
2 changes: 1 addition & 1 deletion Z80/Assembler.Operand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public ushort? NumericValue
get
{
if (!IsNumeric)
throw new Exception(string.Format("Numeric Value Expected; found {0}", RawText));
throw new Exception($"Numeric Value Expected; found {RawText}");

return GetNumericValue(RawText) ?? GetSymbolValue(LineInfo.SymbolTable, LineInfo, RawText);
}
Expand Down
35 changes: 17 additions & 18 deletions Z80/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public partial class Assembler

public bool OK { get; private set; } = false;

private Assembly Assembly = null;
private string Title { get; set; } = null;
private Assembly assembly = null;
private string title = null;
private List<Instruction> instructionSet;

private readonly List<LineInfo> unit = new List<LineInfo>();
private Dictionary<string, LineInfo> symbolTable = new Dictionary<string, LineInfo>();

Expand Down Expand Up @@ -61,7 +60,7 @@ public Assembler(IEnumerable<Instruction> InstructionSet)

public Assembly Assemble(string SourceText)
{
Assembly = new Assembly(SourceText);
assembly = new Assembly(SourceText);
try
{
Assemble();
Expand All @@ -72,7 +71,7 @@ public Assembly Assemble(string SourceText)
OK = false;
throw Ex;
}
return Assembly;
return assembly;
}


Expand All @@ -85,11 +84,11 @@ private void Assemble()
ResolveSymbols();
DetermineData();

Assembly.Finalize(Title, unit, symbolTable, GetSymbolValue(symbolTable, null, "ENTRY") ?? execAddress);
assembly.Finalize(title, unit, symbolTable, GetSymbolValue(symbolTable, null, "ENTRY") ?? execAddress);
}
private void Load()
{
var lines = Assembly.SourceLines.ToList();
var lines = assembly.SourceLines.ToList();

int sourceFileLine = 0;
Macro m = null;
Expand Down Expand Up @@ -156,10 +155,10 @@ private void AddLine(string rawLine, int SourceFileLine, List<LineInfo> Lines =
{
case "TITLE":
var title = lp.Operand0.RawText;
if (!(Title is null))
if (!(this.title is null))
lp.SetError("Title already defined.");
else if (IsValidTitle(ref title))
Title = title;
this.title = title;
else
lp.SetError("Invalid Title");
lp.Suppress();
Expand Down Expand Up @@ -598,14 +597,14 @@ private string PreprocessLine(string Input)
else if (SymbolTable.ContainsKey(s))
{
var symbolLine = SymbolTable[s];
if (symbolLine.Mnemonic == "EQU")
if (symbolLine.HasError)
{
ret = symbolLine.Operand0.NumericValue;
ret += Offset;
CurrentLP?.SetError($"Symbol {Symbol} defined on line {CurrentLP.SourceFileLine} which has an error.");
}
else if (symbolLine.HasError)
else if (symbolLine.Mnemonic == "EQU")
{
CurrentLP?.SetError($"Symbol {Symbol} defined on line {CurrentLP.SourceFileLine} which has an error.");
ret = symbolLine.Operand0.NumericValue;
ret += Offset;
}
else
{
Expand Down Expand Up @@ -648,7 +647,7 @@ private string PreprocessLine(string Input)
string hexString = s;
if (hexString.EndsWith("H"))
hexString = hexString.Substring(0, s.Length - 1);

if (ushort.TryParse(hexString,
System.Globalization.NumberStyles.HexNumber,
System.Globalization.CultureInfo.InvariantCulture,
Expand Down Expand Up @@ -785,10 +784,10 @@ private static string GetLabel(string line)

return GetCol(s, 0).Replace(":", String.Empty).Trim();
}
private static bool IsRegister(string s) => registers.Contains(s);
private static bool IsRegister(string s) => registers.Contains(s);
private static bool IsMetaInstruction(string inst) => metaInstructions.Contains(inst);
private static bool IsInstruction(string inst) => instructionNames.Contains(inst);
private static bool IsFlagState(string s) => flagStates.Contains(s);
private static bool IsInstruction(string inst) => instructionNames.Contains(inst);
private static bool IsFlagState(string s) => flagStates.Contains(s);
private static bool IsValidTitle(ref string Input)
{
Input = Unquote(Input).ToUpper();
Expand Down
2 changes: 1 addition & 1 deletion Z80/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static string ToTwosCompHexString(this byte input)
public static string Truncate(this string Input, int Chars) => Input.Length < Chars ? Input : Input.Substring(0, Chars);

public static bool IsBetween(this char Value, char Min, char Max) => Value >= Min && Value <= Max;
public static bool IsBetween(this int Value, int Min, int Max) => Value >= Min && Value <= Max;
public static bool IsBetween(this int Value, int Min, int Max) => Value >= Min && Value <= Max;
public static bool IsBetween(this byte Value, byte Min, byte Max) => Value >= Min && Value <= Max;

public static char AsAscii(this byte Value)
Expand Down
8 changes: 4 additions & 4 deletions Z80/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static string GetSpacedHex(IReadOnlyList<byte> memory, ushort index, int
case 1:
return $"{memory[index]:X2} ";
case 2:
return $"{memory[index]:X2} {memory[index + 1]:X2} ";
return $"{memory[index]:X2} {memory[(index + 1) & 0xFFFF]:X2} ";
case 3:
return $"{memory[index]:X2} {memory[index + 1]:X2} {memory[index + 2]:X2} ";
return $"{memory[index]:X2} {memory[(index + 1) & 0xFFFF]:X2} {memory[(index + 2) & 0xFFFF]:X2} ";
case 4:
return $"{memory[index]:X2} {memory[index + 1]:X2} {memory[index + 2]:X2} {memory[index + 3]:X2}";
return $"{memory[index]:X2} {memory[(index + 1) & 0xFFFF]:X2} {memory[(index + 2) & 0xFFFF]:X2} {memory[(index + 3) & 0xFFFF]:X2}";
default:
throw new Exception();
throw new Exception("GetSpacedHex: Too many bytes");
}
}
public static ushort HexToUShort(string input) => ushort.Parse(input,
Expand Down
34 changes: 16 additions & 18 deletions Z80/Register.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Sharp80.Z80
internal class Register8 : IRegister<byte>
{
public byte val { get; set; }
public string Name { get; private set; }
public string Name { get; }

public Register8(string Name) => this.Name = Name;

Expand All @@ -21,8 +21,8 @@ internal class Register8 : IRegister<byte>
}
internal class Register8Indirect : IRegister<byte>
{
public IRegister<ushort> Proxy { get; set; }
public string Name { get; private set; }
public IRegister<ushort> Proxy { get; }
public string Name { get; }
private Z80 z80;
public Register8Indirect(Z80 Processor, IRegister<ushort> Proxy, string Name)
{
Expand All @@ -36,17 +36,15 @@ public byte val
set => z80.Memory[Proxy.val] = value;
}

//public ushort ProxyVal { get { return Proxy.val; } }

public void inc() { val++; }
public void dec() { val--; }
public bool NZ { get { return val != 0; } }
public override string ToString() { return val.ToHexString(); }
public void inc() => val++;
public void dec() => val--;
public bool NZ => val != 0;
public override string ToString() => val.ToHexString();
}
internal class RegisterIndexed : IRegisterIndexed
{
public IRegister<ushort> Proxy { get; set; }
public string Name { get; private set; }
public IRegister<ushort> Proxy { get; }
public string Name { get; }
private Z80 z80;

public RegisterIndexed(Z80 Processor, IRegister<ushort> Proxy, string Name)
Expand All @@ -68,7 +66,7 @@ public byte val
}
internal class Register16 : IRegister<ushort>
{
public string Name { get; private set; }
public string Name { get; }
public ushort val { get; set; }

public Register16(string Name) => this.Name = Name;
Expand All @@ -80,9 +78,9 @@ internal class Register16 : IRegister<ushort>
}
internal class RegisterCompound : IRegisterCompound
{
public string Name { get; private set; }
public IRegister<byte> L { get; private set; }
public IRegister<byte> H { get; private set; }
public string Name { get; }
public IRegister<byte> L { get; }
public IRegister<byte> H { get; }

public RegisterCompound(IRegister<byte> Low, IRegister<byte> High, string Name)
{
Expand Down Expand Up @@ -122,7 +120,7 @@ public void dec()
// Used only for the stack pointer indirection
internal class Register16Indirect : IRegister<ushort>
{
public string Name { get; private set; }
public string Name { get; }

private Z80 z80;
private IRegister<ushort> proxy;
Expand All @@ -135,8 +133,8 @@ public Register16Indirect(Z80 Processor, IRegister<ushort> Proxy, string Name)
}
public ushort val
{
get { return z80.Memory.GetWordAt(proxy.val); }
set { z80.Memory.SetWordAt(proxy.val, value); }
get => z80.Memory.GetWordAt(proxy.val);
set => z80.Memory.SetWordAt(proxy.val, value);
}
public void inc() => val++;
public void dec() => val--;
Expand Down
4 changes: 2 additions & 2 deletions Z80/TraceLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -66,7 +66,7 @@ public ushort Log(ulong ElapsedTStates, Func<Instruction, ushort> Exec)
var retVal = Exec(i);

//if (pc == cpu.PcVal) inst += " "; else inst += $"PC {cpu.PcVal:X4} ";
if (a == cpu.AVal) inst += " "; else inst += $"A:{cpu.AVal:X2} ";
if (a == cpu.AVal) inst += " "; else inst += $"A:{cpu.AVal:X2} ";
if (hl == cpu.HlVal) inst += " "; else inst += $"HL:{cpu.HlVal:X4} ";
if (bc == cpu.BcVal) inst += " "; else inst += $"BC:{cpu.BcVal:X4} ";
if (de == cpu.DeVal) inst += " "; else inst += $"DE:{cpu.DeVal:X4} ";
Expand Down
4 changes: 2 additions & 2 deletions Z80/Z80.Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public string GetRealtimeDisassemblyNormal()
return string.Join(Environment.NewLine,
disassemblyAddresses
.Select(i =>
new { addr = disassemblyAddresses[j++] = startLocation })
.Select(n => GetLineInfo((n.addr == PC.val) ? ">" : " ", ref startLocation, GetInstructionAt(n.addr))));
new { addr = disassemblyAddresses[j++] = startLocation })
.Select(n => GetLineInfo((n.addr == PC.val) ? ">" : " ", ref startLocation, GetInstructionAt(n.addr))));
}
public string GetRealtimeDisassemblyHistoric()
{
Expand Down
Loading

0 comments on commit 4291ffa

Please sign in to comment.