From 31203bc854656ff92844364903de9abee9daea8b Mon Sep 17 00:00:00 2001 From: Ian C Date: Wed, 7 Mar 2012 22:48:11 +0000 Subject: Safe-keeping commit. Currently debugging emulation using FUSE tests. --- .../TestMachine.cs | 25 +- .../Z80Disassembler.cs | 451 ++++++++++++--------- src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs | 12 +- .../Z80CpuBaseOpcodes.cs | 10 +- .../Z80CpuDecodeByte.cs | 7 +- 5 files changed, 284 insertions(+), 221 deletions(-) diff --git a/native/Noddybox.Emulation.EightBit.Z80.Test/TestMachine.cs b/native/Noddybox.Emulation.EightBit.Z80.Test/TestMachine.cs index a876826..d21e810 100644 --- a/native/Noddybox.Emulation.EightBit.Z80.Test/TestMachine.cs +++ b/native/Noddybox.Emulation.EightBit.Z80.Test/TestMachine.cs @@ -56,25 +56,25 @@ namespace Noddybox.Emulation.EightBit.Z80.Test byte IMemory.Read(ushort address) { - Output(ConsoleColor.Green, ConsoleColor.Black, "Reading {0:X2} from {1:X4}", mem[address], address); + // Output(ConsoleColor.Green, ConsoleColor.Black, "Reading {0:X2} from {1:X4}", mem[address], address); return mem[address]; } void IMemory.Write(ushort address, byte value) { - Output(ConsoleColor.Red, ConsoleColor.Black, "Writing {0:X2} to {1:X4}", value, address); + // Output(ConsoleColor.Red, ConsoleColor.Black, "Writing {0:X2} to {1:X4}", value, address); mem[address] = value; } byte IDevice.Read(ushort device) { - Output(ConsoleColor.Green, ConsoleColor.DarkGray, "Reading 00 from device {1:X4}", device); + // Output(ConsoleColor.Green, ConsoleColor.DarkGray, "Reading 00 from device {1:X4}", device); return 0; } void IDevice.Write(ushort device, byte value) { - Output(ConsoleColor.Red, ConsoleColor.DarkGray, "Writing {0:X2} to device {1:X4}", value, device); + // Output(ConsoleColor.Red, ConsoleColor.DarkGray, "Writing {0:X2} to device {1:X4}", value, device); } public bool Run(string name, Queue input, Queue expected) @@ -110,6 +110,8 @@ namespace Noddybox.Emulation.EightBit.Z80.Test z80.IM_Register = line.Dequeue().low; z80.HaltLine = (line.Dequeue().reg != 0); + Output(ConsoleColor.Gray, ConsoleColor.Black, z80.ToString()); + int cyclesToRun = Convert.ToInt32(line.Dequeue().reg.ToString("X")); while(input.Count > 0) @@ -118,9 +120,8 @@ namespace Noddybox.Emulation.EightBit.Z80.Test if (line.Count > 1) { - ushort start; ushort addr = line.Dequeue().reg; - start = addr; + ushort start = addr; foreach (Register16 b in line) { @@ -131,26 +132,24 @@ namespace Noddybox.Emulation.EightBit.Z80.Test { string a, b, c; start = disassembler.Disassemble(start, out a, out b, out c); - Output(ConsoleColor.Yellow, ConsoleColor.Blue, "{0}: {1} ; {2}", a, b, c); + Output(ConsoleColor.Yellow, ConsoleColor.Blue, "{0}: {1}", a, b); } } } - if (Debugger.IsAttached && name == "09") - { - Debugger.Break(); - } - while (clock.Ticks < cyclesToRun) { z80.Step(); } + Output(ConsoleColor.White, ConsoleColor.Black, z80.ToString()); + bool ok = true; while(expected.Peek().StartsWith(" ")) { - Output(ConsoleColor.DarkGray, ConsoleColor.Black, "Not yet handled - {0}", expected.Dequeue()); + // Output(ConsoleColor.DarkGray, ConsoleColor.Black, "Not yet handled - {0}", expected.Dequeue()); + expected.Dequeue(); } line = Decode(expected.Dequeue()); diff --git a/src/Noddybox.Emulation.EightBit.Z80.Disassembler/Z80Disassembler.cs b/src/Noddybox.Emulation.EightBit.Z80.Disassembler/Z80Disassembler.cs index cdb378d..438b962 100644 --- a/src/Noddybox.Emulation.EightBit.Z80.Disassembler/Z80Disassembler.cs +++ b/src/Noddybox.Emulation.EightBit.Z80.Disassembler/Z80Disassembler.cs @@ -16,6 +16,7 @@ // Copyright (c) 2012 Ian Cowburn // using System; +using System.Text; namespace Noddybox.Emulation.EightBit.Z80.Disassembler { @@ -45,19 +46,28 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler #endregion - #region Private. Some of these routines are to support the original C code. + #region Private routines to support the original C code. private byte Z80_Dis_FetchByte(ref ushort addr) { - return memory.Read(addr++); + byte b = memory.Read(addr++); + + if (Hex.Length > 0) + { + Hex.Append(" "); + } + + Hex.AppendFormat("{0:x2}", b); + + return b; } - private ushort FetchWord(ref ushort addr) + private ushort Z80_Dis_FetchWord(ref ushort addr) { Register16 r = new Register16(0); - r.low = memory.Read(addr++); - r.high = memory.Read(addr++); + r.low = Z80_Dis_FetchByte(ref addr); + r.high = Z80_Dis_FetchByte(ref addr); return r.reg; } @@ -65,15 +75,75 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private string Opcode {get; set;} private string Argument {get; set;} + private StringBuilder Hex {get; set;} + private void Z80_Dis_Set(string a, string b) { Opcode = a; Argument = b; } + private string PopString(string s, int count) + { + if (s.Length <= count) + { + return String.Empty; + } + else + { + return s.Substring(count); + } + } + + private string Z80_Dis_Printf(string fmt, params object[] o) + { + StringBuilder sb = new StringBuilder(); + int arg = 0; + + while(fmt.Length > 0) + { + if (fmt[0] == '%') + { + if (fmt[1] == 'd' || fmt[1] == 's') + { + sb.Append("{"); + sb.AppendFormat("{0}", arg++); + sb.Append("}"); + fmt = PopString(fmt, 2); + } + else if (fmt.StartsWith("%.2x")) + { + sb.Append("{"); + sb.AppendFormat("{0}:x2", arg++); + sb.Append("}"); + fmt = PopString(fmt, 4); + } + else if (fmt.StartsWith("%.4x")) + { + sb.Append("{"); + sb.AppendFormat("{0}:x4", arg++); + sb.Append("}"); + fmt = PopString(fmt, 4); + } + else + { + sb.Append(fmt[0]); + fmt = PopString(fmt, 1); + } + } + else + { + sb.Append(fmt[0]); + fmt = PopString(fmt, 1); + } + } + + return String.Format(sb.ToString(), o); + } + #endregion - #region Disassembler functions. This code pretty much pasted as-is from my original C version + #region Disassembler functions. This code pretty much pasted as-is from my original C version, with support routines to help. /* ---------------------------------------- CB xx BYTE OPCODES */ @@ -178,7 +248,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { sbyte r; - r=(sbyte)Z80_Dis_FetchByte(z80,pc); + r=(sbyte)Z80_Dis_FetchByte(ref pc); if (r<0) return String.Format("(ix-{0:x2})",-r); @@ -206,37 +276,28 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { case 0: return ("b"); - break; case 1: return ("c"); - break; case 2: return ("d"); - break; case 3: return ("e"); - break; case 4: return ("ixh"); - break; case 5: return ("ixl"); - break; case 6: - return (IX_RelStr(0, pc)); - break; + return (IX_RelStr(0, ref pc)); case 7: return ("a"); - break; default: return String.Format("BUG {0}", reg); - break; } } private void DIS_DD_NOP(byte op, ref ushort pc) { - dis_opcode_z80[op](op, pc); + dis_opcode_z80[op](op, ref pc); } private void DIS_ADD_IX_BC(byte op, ref ushort pc) @@ -251,14 +312,14 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IX_WORD(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("ix,$%.4x", Z80_Dis_FetchWord(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("ix,$%.4x", Z80_Dis_FetchWord(ref pc))); } private void DIS_LD_ADDR_IX(byte op, ref ushort pc) { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("($%.4x),ix", w)); } @@ -279,7 +340,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IXH_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("ixh,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("ixh,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_ADD_IX_IX(byte op, ref ushort pc) @@ -291,7 +352,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("ix,($%.4x)", w)); } @@ -312,17 +373,17 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IXL_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("ixl,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("ixl,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_INC_IIX(byte op, ref ushort pc) { - Z80_Dis_Set("inc", Z80_Dis_Printf("%s", IX_RelStr(op, pc))); + Z80_Dis_Set("inc", Z80_Dis_Printf("%s", IX_RelStr(op, ref pc))); } private void DIS_DEC_IIX(byte op, ref ushort pc) { - Z80_Dis_Set("dec", Z80_Dis_Printf("%s", IX_RelStr(op, pc))); + Z80_Dis_Set("dec", Z80_Dis_Printf("%s", IX_RelStr(op, ref pc))); } private void DIS_LD_IIX_BYTE(byte op, ref ushort pc) @@ -330,8 +391,8 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string rel; int b; - rel = IX_RelStr(op, pc); - b = Z80_Dis_FetchByte(pc); + rel = IX_RelStr(op, ref pc); + b = Z80_Dis_FetchByte(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.2x", rel, b)); } @@ -352,62 +413,62 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler /* IX can't be used as source and destination when reading z80ory */ if (dest_r==6) - { - dest=XR8(dest_r,pc); + { + dest=XR8(dest_r, ref pc); src=z80_dis_reg8[src_r]; - } + } else if (((dest_r==4)||(dest_r==5))&&(src_r==6)) - { + { dest=z80_dis_reg8[dest_r]; - src=XR8(src_r,pc); - } + src = XR8(src_r, ref pc); + } else - { - dest=XR8(dest_r,pc); - src=XR8(src_r,pc); - } + { + dest = XR8(dest_r, ref pc); + src = XR8(src_r, ref pc); + } Z80_Dis_Set("ld",Z80_Dis_Printf("%s,%s",dest,src)); } private void DIS_XADD_R8(byte op, ref ushort pc) { - Z80_Dis_Set("add", Z80_Dis_Printf("a,%s", XR8((op % 8), pc))); + Z80_Dis_Set("add", Z80_Dis_Printf("a,%s", XR8((op % 8), ref pc))); } private void DIS_XADC_R8(byte op, ref ushort pc) { - Z80_Dis_Set("adc", Z80_Dis_Printf("a,%s", XR8((op % 8), pc))); + Z80_Dis_Set("adc", Z80_Dis_Printf("a,%s", XR8((op % 8), ref pc))); } private void DIS_XSUB_R8(byte op, ref ushort pc) { - Z80_Dis_Set("sub", Z80_Dis_Printf("a,%s", XR8((op % 8), pc))); + Z80_Dis_Set("sub", Z80_Dis_Printf("a,%s", XR8((op % 8), ref pc))); } private void DIS_XSBC_R8(byte op, ref ushort pc) { - Z80_Dis_Set("sbc", Z80_Dis_Printf("a,%s", XR8((op % 8), pc))); + Z80_Dis_Set("sbc", Z80_Dis_Printf("a,%s", XR8((op % 8), ref pc))); } private void DIS_XAND_R8(byte op, ref ushort pc) { - Z80_Dis_Set("and", Z80_Dis_Printf("%s", XR8((op % 8), pc))); + Z80_Dis_Set("and", Z80_Dis_Printf("%s", XR8((op % 8), ref pc))); } private void DIS_XXOR_R8(byte op, ref ushort pc) { - Z80_Dis_Set("xor", Z80_Dis_Printf("%s", XR8((op % 8), pc))); + Z80_Dis_Set("xor", Z80_Dis_Printf("%s", XR8((op % 8), ref pc))); } private void DIS_X_OR_R8(byte op, ref ushort pc) { - Z80_Dis_Set("or", Z80_Dis_Printf("%s", XR8((op % 8), pc))); + Z80_Dis_Set("or", Z80_Dis_Printf("%s", XR8((op % 8), ref pc))); } private void DIS_XCP_R8(byte op, ref ushort pc) { - Z80_Dis_Set("cp", Z80_Dis_Printf("%s", XR8((op % 8), pc))); + Z80_Dis_Set("cp", Z80_Dis_Printf("%s", XR8((op % 8), ref pc))); } private void DIS_POP_IX(byte op, ref ushort pc) @@ -437,35 +498,35 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_DD_CB_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - cb_off = (sbyte)Z80_Dis_FetchByte(pc); - nop = Z80_Dis_FetchByte(pc); - dis_DD_CB_opcode[nop](nop, pc); + cb_off = (sbyte)Z80_Dis_FetchByte(ref pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_DD_CB_opcode[nop](nop, ref pc); } private void DIS_DD_DD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_DD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_DD_opcode[nop](nop, ref pc); } private void DIS_DD_ED_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_ED_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_ED_opcode[nop](nop, ref pc); } private void DIS_DD_FD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_FD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_FD_opcode[nop](nop, ref pc); } @@ -479,10 +540,10 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rlc", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("rlc", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else { - Z80_Dis_Set("rlc", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rlc", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } } @@ -493,9 +554,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rrc", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("rrc", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rrc", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rrc", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RL_IX(byte op, ref ushort pc) @@ -505,9 +566,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rl", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("rl", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rl", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rl", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RR_IX(byte op, ref ushort pc) @@ -517,9 +578,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rr", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("rr", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rr", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rr", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SLA_IX(byte op, ref ushort pc) @@ -529,9 +590,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sla", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("sla", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sla", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sla", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SRA_IX(byte op, ref ushort pc) @@ -541,9 +602,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sra", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("sra", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sra", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sra", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SRL_IX(byte op, ref ushort pc) @@ -553,9 +614,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("srl", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("srl", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("srl", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("srl", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SLL_IX(byte op, ref ushort pc) @@ -565,9 +626,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sll", Z80_Dis_Printf("%s", IX_RelStrCB(op, pc))); + Z80_Dis_Set("sll", Z80_Dis_Printf("%s", IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sll", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sll", Z80_Dis_Printf("%s[%s]", IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_BIT_IX(byte op, ref ushort pc) @@ -579,9 +640,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0x40) / 8; if (reg == 6) - Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, pc))); + Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RES_IX(byte op, ref ushort pc) @@ -593,9 +654,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0x80) / 8; if (reg == 6) - Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, pc))); + Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SET_IX(byte op, ref ushort pc) @@ -607,9 +668,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0xc0) / 8; if (reg == 6) - Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, pc))); + Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s", bit, IX_RelStrCB(op, ref pc))); else - Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s[%s]", bit, IX_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } @@ -622,28 +683,20 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { case 0: return ("b"); - break; case 1: return ("c"); - break; case 2: return ("d"); - break; case 3: return ("e"); - break; case 4: return ("h"); - break; case 5: return ("l"); - break; case 6: return ("0"); - break; case 7: return ("a"); - break; } return "?"; @@ -675,7 +728,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("($%.4x),%s", w, z80_dis_reg16[(op - 0x40) / 16])); } @@ -708,7 +761,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("%s,($%.4x)", z80_dis_reg16[(op - 0x40) / 16], w)); } @@ -840,7 +893,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { sbyte r; - r=(sbyte)Z80_Dis_FetchByte(z80,pc); + r=(sbyte)Z80_Dis_FetchByte(ref pc); if (r<0) return String.Format("(iy-${0:x2})",-r); @@ -868,37 +921,28 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { case 0: return ("b"); - break; case 1: return ("c"); - break; case 2: return ("d"); - break; case 3: return ("e"); - break; case 4: return ("iyh"); - break; case 5: return ("iyl"); - break; case 6: - return (IY_RelStr(0, pc)); - break; + return (IY_RelStr(0, ref pc)); case 7: return ("a"); - break; default: return String.Format("BUG {0}", reg); - break; } } private void DIS_FD_NOP(byte op, ref ushort pc) { - dis_opcode_z80[op](op, pc); + dis_opcode_z80[op](op, ref pc); } private void DIS_ADD_IY_BC(byte op, ref ushort pc) @@ -913,14 +957,14 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IY_WORD(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("iy,$%.4x", Z80_Dis_FetchWord(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("iy,$%.4x", Z80_Dis_FetchWord(ref pc))); } private void DIS_LD_ADDR_IY(byte op, ref ushort pc) { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("($%.4x),iy", w)); } @@ -941,7 +985,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IYH_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("iyh,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("iyh,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_ADD_IY_IY(byte op, ref ushort pc) @@ -953,7 +997,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("iy,($%.4x)", w)); } @@ -974,17 +1018,17 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_LD_IYL_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("ld", Z80_Dis_Printf("iyl,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("iyl,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_INC_IIY(byte op, ref ushort pc) { - Z80_Dis_Set("inc", Z80_Dis_Printf("%s", IY_RelStr(op, pc))); + Z80_Dis_Set("inc", Z80_Dis_Printf("%s", IY_RelStr(op, ref pc))); } private void DIS_DEC_IIY(byte op, ref ushort pc) { - Z80_Dis_Set("dec", Z80_Dis_Printf("%s", IY_RelStr(op, pc))); + Z80_Dis_Set("dec", Z80_Dis_Printf("%s", IY_RelStr(op, ref pc))); } private void DIS_LD_IIY_BYTE(byte op, ref ushort pc) @@ -992,8 +1036,8 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string rel; int b; - rel = IY_RelStr(op, pc); - b = Z80_Dis_FetchByte(pc); + rel = IY_RelStr(op, ref pc); + b = Z80_Dis_FetchByte(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.2x", rel, b)); } @@ -1015,18 +1059,18 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler */ if (dest_r==6) { - dest=YR8(z80,dest_r,pc); + dest=YR8(dest_r,ref pc); src=z80_dis_reg8[src_r]; } else if (((dest_r==4)||(dest_r==5))&&(src_r==6)) { dest=z80_dis_reg8[dest_r]; - src=YR8(z80,src_r,pc); + src=YR8(src_r,ref pc); } else { - dest=YR8(z80,dest_r,pc); - src=YR8(z80,src_r,pc); + dest=YR8(dest_r,ref pc); + src = YR8(src_r, ref pc); } Z80_Dis_Set("ld",Z80_Dis_Printf("%s,%s",dest,src)); @@ -1034,42 +1078,42 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_YADD_R8(byte op, ref ushort pc) { - Z80_Dis_Set("add", Z80_Dis_Printf("a,%s", YR8((op % 8), pc))); + Z80_Dis_Set("add", Z80_Dis_Printf("a,%s", YR8((op % 8), ref pc))); } private void DIS_YADC_R8(byte op, ref ushort pc) { - Z80_Dis_Set("adc", Z80_Dis_Printf("a,%s", YR8((op % 8), pc))); + Z80_Dis_Set("adc", Z80_Dis_Printf("a,%s", YR8((op % 8), ref pc))); } private void DIS_YSUB_R8(byte op, ref ushort pc) { - Z80_Dis_Set("sub", Z80_Dis_Printf("a,%s", YR8((op % 8), pc))); + Z80_Dis_Set("sub", Z80_Dis_Printf("a,%s", YR8((op % 8), ref pc))); } private void DIS_YSBC_R8(byte op, ref ushort pc) { - Z80_Dis_Set("sbc", Z80_Dis_Printf("a,%s", YR8((op % 8), pc))); + Z80_Dis_Set("sbc", Z80_Dis_Printf("a,%s", YR8((op % 8), ref pc))); } private void DIS_YAND_R8(byte op, ref ushort pc) { - Z80_Dis_Set("and", Z80_Dis_Printf("%s", YR8((op % 8), pc))); + Z80_Dis_Set("and", Z80_Dis_Printf("%s", YR8((op % 8), ref pc))); } private void DIS_YYOR_R8(byte op, ref ushort pc) { - Z80_Dis_Set("xor", Z80_Dis_Printf("%s", YR8((op % 8), pc))); + Z80_Dis_Set("xor", Z80_Dis_Printf("%s", YR8((op % 8), ref pc))); } private void DIS_Y_OR_R8(byte op, ref ushort pc) { - Z80_Dis_Set("or", Z80_Dis_Printf("%s", YR8((op % 8), pc))); + Z80_Dis_Set("or", Z80_Dis_Printf("%s", YR8((op % 8), ref pc))); } private void DIS_YCP_R8(byte op, ref ushort pc) { - Z80_Dis_Set("cp", Z80_Dis_Printf("%s", YR8((op % 8), pc))); + Z80_Dis_Set("cp", Z80_Dis_Printf("%s", YR8((op % 8), ref pc))); } private void DIS_POP_IY(byte op, ref ushort pc) @@ -1099,35 +1143,35 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_FD_CB_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - cb_off = (sbyte)Z80_Dis_FetchByte(pc); - nop = Z80_Dis_FetchByte(pc); - dis_FD_CB_opcode[nop](nop, pc); + cb_off = (sbyte)Z80_Dis_FetchByte(ref pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_FD_CB_opcode[nop](nop, ref pc); } private void DIS_FD_DD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_DD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_DD_opcode[nop](nop, ref pc); } private void DIS_FD_ED_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_ED_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_ED_opcode[nop](nop, ref pc); } private void DIS_FD_FD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_FD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_FD_opcode[nop](nop, ref pc); } @@ -1141,9 +1185,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rlc", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("rlc", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rlc", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rlc", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RRC_IY(byte op, ref ushort pc) @@ -1153,9 +1197,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rrc", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("rrc", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rrc", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rrc", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RL_IY(byte op, ref ushort pc) @@ -1165,9 +1209,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rl", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("rl", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rl", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rl", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RR_IY(byte op, ref ushort pc) @@ -1177,9 +1221,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("rr", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("rr", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("rr", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("rr", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SLA_IY(byte op, ref ushort pc) @@ -1189,9 +1233,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sla", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("sla", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sla", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sla", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SRA_IY(byte op, ref ushort pc) @@ -1201,9 +1245,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sra", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("sra", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sra", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sra", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SRL_IY(byte op, ref ushort pc) @@ -1213,9 +1257,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("srl", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("srl", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("srl", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("srl", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SLL_IY(byte op, ref ushort pc) @@ -1225,9 +1269,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = (op % 8); if (reg == 6) - Z80_Dis_Set("sll", Z80_Dis_Printf("%s", IY_RelStrCB(op, pc))); + Z80_Dis_Set("sll", Z80_Dis_Printf("%s", IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("sll", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("sll", Z80_Dis_Printf("%s[%s]", IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_BIT_IY(byte op, ref ushort pc) @@ -1239,9 +1283,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0x40) / 8; if (reg == 6) - Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, pc))); + Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("bit", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_RES_IY(byte op, ref ushort pc) @@ -1253,9 +1297,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0x80) / 8; if (reg == 6) - Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, pc))); + Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("res", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } private void DIS_SET_IY(byte op, ref ushort pc) @@ -1267,9 +1311,9 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler bit = (op - 0xc0) / 8; if (reg == 6) - Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, pc))); + Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s", bit, IY_RelStrCB(op, ref pc))); else - Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, pc), z80_dis_reg8[reg])); + Z80_Dis_Set("set", Z80_Dis_Printf("%d,%s[%s]", bit, IY_RelStrCB(op, ref pc), z80_dis_reg8[reg])); } @@ -1285,7 +1329,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string reg; reg = z80_dis_reg16[(op & 0x30) / 0x10]; - Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.4x", reg, Z80_Dis_FetchWord(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.4x", reg, Z80_Dis_FetchWord(ref pc))); } private void DIS_LD_R16_A(byte op, ref ushort pc) @@ -1325,7 +1369,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string reg; reg = z80_dis_reg8[(op & 0x38) / 0x8]; - Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.2x", reg, Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("ld", Z80_Dis_Printf("%s,$%.2x", reg, Z80_Dis_FetchByte(ref pc))); } private void DIS_RLCA(byte op, ref ushort pc) @@ -1371,7 +1415,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort n; - n = pc + (sbyte)memory.Read(pc) + 1; + n = (ushort)(pc + (sbyte)((memory.Read(pc) + 1))); pc++; Z80_Dis_Set("djnz", Z80_Dis_Printf("$%.4x", n)); } @@ -1385,7 +1429,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort n; - n = pc + (sbyte)memory.Read(pc) + 1; + n = (ushort)(pc + (sbyte)(memory.Read(pc) + 1)); pc++; Z80_Dis_Set("jr", Z80_Dis_Printf("$%.4x", n)); } @@ -1401,7 +1445,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler ushort n; con = z80_dis_condition[(op - 0x20) / 8]; - n = pc + (sbyte)memory.Read(pc) + 1; + n = (ushort)(pc + (sbyte)(memory.Read(pc) + 1)); pc++; Z80_Dis_Set("jr", Z80_Dis_Printf("%s,$%.4x", con, n)); } @@ -1410,7 +1454,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("($%.4x),hl", w)); } @@ -1423,7 +1467,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("hl,($%.4x)", w)); } @@ -1436,7 +1480,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("($%.4x),a", w)); } @@ -1449,7 +1493,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("ld", Z80_Dis_Printf("a,($%.4x)", w)); } @@ -1551,7 +1595,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = z80_dis_reg16[(op - 0xc0) / 16]; - if (!strcmp(reg, "sp")) + if (reg == "sp") reg = "af"; Z80_Dis_Set("pop", reg); @@ -1561,7 +1605,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("jp", Z80_Dis_Printf("$%.4x", w)); } @@ -1571,7 +1615,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler reg = z80_dis_reg16[(op - 0xc0) / 16]; - if (!strcmp(reg, "sp")) + if (reg == "sp") reg = "af"; Z80_Dis_Set("push", reg); @@ -1579,7 +1623,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_ADD_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("add", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("add", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_RST(byte op, ref ushort pc) @@ -1600,17 +1644,17 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string con; ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); con = z80_dis_condition[(op - 0xc0) / 8]; Z80_Dis_Set("jp", Z80_Dis_Printf("%s,$%.4x", con, w)); } private void DIS_CB_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_CB_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_CB_opcode[nop](nop, ref pc); } private void DIS_CALL_CO(byte op, ref ushort pc) @@ -1618,7 +1662,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler string con; ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); con = z80_dis_condition[(op - 0xc0) / 8]; Z80_Dis_Set("call", Z80_Dis_Printf("%s,$%.4x", con, w)); } @@ -1627,23 +1671,23 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler { ushort w; - w = Z80_Dis_FetchWord(pc); + w = Z80_Dis_FetchWord(ref pc); Z80_Dis_Set("call", Z80_Dis_Printf("$%.4x", w)); } private void DIS_ADC_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("adc", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("adc", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_OUT_BYTE_A(byte op, ref ushort pc) { - Z80_Dis_Set("out", Z80_Dis_Printf("($%.2x),a", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("out", Z80_Dis_Printf("($%.2x),a", Z80_Dis_FetchByte(ref pc))); } private void DIS_SUB_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("sub", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("sub", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_EXX(byte op, ref ushort pc) @@ -1653,20 +1697,20 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_IN_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("in", Z80_Dis_Printf("a,($%.2x)", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("in", Z80_Dis_Printf("a,($%.2x)", Z80_Dis_FetchByte(ref pc))); } private void DIS_DD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_DD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_DD_opcode[nop](nop, ref pc); } private void DIS_SBC_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("sbc", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("sbc", Z80_Dis_Printf("a,$%.2x", Z80_Dis_FetchByte(ref pc))); } @@ -1677,7 +1721,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_AND_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("and", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("and", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_JP_HL(byte op, ref ushort pc) @@ -1692,15 +1736,15 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_ED_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_ED_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_ED_opcode[nop](nop, ref pc); } private void DIS_XOR_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("xor", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("xor", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_DI(byte op, ref ushort pc) @@ -1710,7 +1754,7 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_OR_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("or", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("or", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(ref pc))); } private void DIS_LD_SP_HL(byte op, ref ushort pc) @@ -1725,15 +1769,15 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler private void DIS_FD_DECODE(byte op, ref ushort pc) { - int nop; + byte nop; - nop = Z80_Dis_FetchByte(pc); - dis_FD_opcode[nop](nop, pc); + nop = Z80_Dis_FetchByte(ref pc); + dis_FD_opcode[nop](nop, ref pc); } private void DIS_CP_A_BYTE(byte op, ref ushort pc) { - Z80_Dis_Set("cp", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(pc))); + Z80_Dis_Set("cp", Z80_Dis_Printf("$%.2x", Z80_Dis_FetchByte(ref pc))); } #endregion @@ -1746,6 +1790,8 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler // Register16.Verify(); + Hex = new StringBuilder(); + // Setup function tables // dis_CB_opcode = new disassemble[0x100] @@ -2355,13 +2401,16 @@ namespace Noddybox.Emulation.EightBit.Z80.Disassembler public ushort Disassemble(ushort startAddress, out string address, out string opcode, out string hexdump) { - address = String.Empty; - opcode = String.Empty; - hexdump = String.Empty; - Opcode = String.Empty; - Argument = String.Empty; + address = String.Format("{0:X4}", startAddress); + + byte op = Z80_Dis_FetchByte(ref startAddress); + + Hex.Clear(); + + dis_opcode_z80[op](op, ref startAddress); - String.Format(address, "{0:X4}", address); + opcode = String.Format("{0} {1}", Opcode, Argument); + hexdump = Hex.ToString(); return startAddress; } diff --git a/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs b/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs index a2e6fed..2da155e 100644 --- a/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs +++ b/src/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs @@ -242,7 +242,7 @@ namespace Noddybox.Emulation.EightBit.Z80 } } - if ((parity & 1) == 1) + if ((parity & 1) == 0) { p = Z80Flags.PV; } @@ -523,5 +523,15 @@ namespace Noddybox.Emulation.EightBit.Z80 } #endregion + + #region Object overrides + + public override string ToString() + { + return String.Format("A={0:x2} BC={1:x4} DE={2:x4} HL={3:x4} F={4}\nPC={5:x4} SP={6:x4} IX={7:x4} IY={8:x4} AF'={9:x4} BC'={10:x4} DE'={11:x4} HL'={12:x4}\nI={13:x2} R={14:x2} IFF1={15} IFF2={16} IM={17} HALT={18}", + A, BC.reg, DE.reg, HL.reg, F, PC, SP, IX.reg, IY.reg, AF_.reg, BC_.reg, DE_.reg, HL_.reg, I, R, IFF1, IFF2, IM, HALT); + } + + #endregion } } diff --git a/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs index bc09ce2..e0cc4e4 100644 --- a/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs +++ b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuBaseOpcodes.cs @@ -441,9 +441,13 @@ namespace Noddybox.Emulation.EightBit.Z80 F = PSZtable[A] | carry - | nf | - ((Z80Flags)(acc ^ A) & Z80Flags.HalfCarry) + | nf | H35table[A]; + + if (((A ^ acc) & 0x10) == 0x10) + { + F |= Z80Flags.HalfCarry; + } } #endregion @@ -517,7 +521,7 @@ namespace Noddybox.Emulation.EightBit.Z80 byte carry = (byte)(F & Z80Flags.Carry); F = (F & (Z80Flags.PV | Z80Flags.Sign | Z80Flags.Zero)) | (Z80Flags)Binary.ShiftRight(A, 7); - A = (byte)(Binary.ShiftRight(A, 1) | carry); + A = (byte)(Binary.ShiftLeft(A, 1) | carry); F |= H35table[A]; } diff --git a/src/Noddybox.Emulation.EightBit.Z80/Z80CpuDecodeByte.cs b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuDecodeByte.cs index f0256ba..22a6c98 100644 --- a/src/Noddybox.Emulation.EightBit.Z80/Z80CpuDecodeByte.cs +++ b/src/Noddybox.Emulation.EightBit.Z80/Z80CpuDecodeByte.cs @@ -141,7 +141,7 @@ namespace Noddybox.Emulation.EightBit.Z80 break; case 0x10: // DJNZ - if (--BC.high == 0) + if (--BC.high != 0) { clock.Add(13); JR(); @@ -279,7 +279,7 @@ namespace Noddybox.Emulation.EightBit.Z80 break; case 0x2a: // LD HL,(nnnnn) - clock.Add(7); + clock.Add(16); addr = FetchWord(); hl.low = memory.Read(addr); hl.high = memory.Read((ushort)(addr + 1)); @@ -410,6 +410,7 @@ namespace Noddybox.Emulation.EightBit.Z80 F ^= Z80Flags.Carry; ClearFlag(Z80Flags.Hidden); + ClearFlag(Z80Flags.Neg); SetFlag(H35table[A]); break; @@ -734,7 +735,7 @@ namespace Noddybox.Emulation.EightBit.Z80 case 0x7e: // LD A,(HL) clock.Add(7); addr = (ushort)(hl.reg + Offset()); - HL.low = memory.Read(addr); + A = memory.Read(addr); break; case 0x7f: // LD A,A -- cgit v1.2.3