-
Notifications
You must be signed in to change notification settings - Fork 3
/
walk6502.cs
155 lines (146 loc) · 5.78 KB
/
walk6502.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// walk6502.cs - Class Walk6502
// disassembly of all reachable executable code including branches, jump, calls
//
////////////////////////////////////////////////////////////////////////////////
//
// simple-emu-c64
// C64/6502 Emulator for Microsoft Windows Console
//
// MIT License
//
// Copyright (c) 2020-2023 by David R. Van Wagner
// davevw.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace simple_emu_c64
{
class Walk6502
{
static HashSet<ushort> seen = new HashSet<ushort>();
public static void Reset()
{
seen.Clear();
}
public static void Walk(Emu6502 cpu, ushort addr)
{
bool conditional;
byte bytes;
ushort addr2;
HashSet<ushort> branches = new HashSet<ushort>();
while (true)
{
if (seen.Contains(addr))
{
while (true)
{
if (branches.Count == 0)
return; // done with walk
else
{
addr = branches.First(); // walk a saved address
branches.Remove(addr);
if (!seen.Contains(addr))
break;
}
}
}
string line;
string dis = cpu.Disassemble(addr, out conditional, out bytes, out addr2, out line);
Console.WriteLine(line);
if (dis != "???")
seen.Add(addr);
switch (dis)
{
case "BRK":
case "RTI":
case "RTS":
case "???":
if (branches.Count == 0)
return; // done with walk
else
{
addr = branches.First(); // walk a saved address
branches.Remove(addr);
break;
}
default:
if (!conditional && addr2 != 0)
{
if (dis.StartsWith("JSR"))
{
Walk(cpu, addr2); // walk call recursively, then continue next address
addr += bytes;
if ((addr2 == 0xFF7D && cpu is EmuC128)
|| ((addr2 == 0xFF4F || addr2 == 0xFBD8) && cpu is EmuTED))
{
addr += DisplayStringZ(cpu, addr); // Print Immediate
}
}
else
addr = addr2;
}
else
{
addr += bytes;
if (conditional && !seen.Contains(addr2) && !branches.Contains(addr2))
branches.Add(addr2); // save branch address for later
}
break;
}
}
}
static char[] space_separator = new char[] { ' ' };
static string Unprintables = "{}\\|~";
private static ushort DisplayStringZ(Emu6502 cpu, ushort addr)
{
ushort count = 0;
Console.Write($"{addr:X4}");
StringBuilder s = new StringBuilder();
while (true)
{
cpu.Disassemble(addr, out bool conditional, out var bytes, out var addr2, out var line);
var data = line.Substring(5, 8).Split(space_separator, StringSplitOptions.RemoveEmptyEntries);
foreach (string hex in data)
{
Console.Write($" {hex}");
var value = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
if (value >= 32 && value < 127 && !Unprintables.Contains((char)value))
s.Append((char)value);
else if (value != 0)
s.Append('~');
++count;
if (value == 0)
{
if (s.Length > 0)
Console.Write($" '{s}");
Console.WriteLine();
return count;
}
++addr;
}
}
}
}
}