-
Notifications
You must be signed in to change notification settings - Fork 4
/
emucbm.cs
236 lines (215 loc) · 7.68 KB
/
emucbm.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace simple_emu_c64
{
public class EmuCBM : Emu6502
{
protected static readonly ConsoleColor startup_fg = Console.ForegroundColor;
protected static readonly ConsoleColor startup_bg = Console.BackgroundColor;
protected string FileName = null;
protected byte FileNum = 0;
protected byte FileDev = 0;
protected byte FileSec = 0;
protected bool FileVerify = false;
protected ushort FileAddr = 0;
protected int LOAD_TRAP = -1;
public EmuCBM(Emu6502.Memory memory):base(memory)
{
}
public string StartupPRG
{
get;
set;
}
protected override bool ExecutePatch()
{
if (PC == 0xFFD2) // CHROUT
{
CBM_Console.WriteChar((char)A);
}
else if (PC == 0xFFCF) // CHRIN
{
SetA(CBM_Console.ReadChar());
C = false;
return ExecuteRTS();
}
else if (PC == 0xFFE4) // GETIN
{
//BASIC TEST:
//10 GET K$ : REM GETIN
//20 IF K$<> "" THEN PRINT ASC(K$)
//25 IF K$= "Q" THEN END
//30 GOTO 10
C = false;
SetA(CBM_Console.GetIn());
if (A != 0)
X = A; // observed this side effect from tracing code, so replicating
return ExecuteRTS();
}
else if (PC == 0xFFE1) // STOP
{
Z = CBM_Console.CheckStop();
return ExecuteRTS();
}
else if (PC == 0xFFBA) // SETLFS
{
FileNum = A;
FileDev = X;
FileSec = Y;
System.Diagnostics.Debug.WriteLine(string.Format("SETLFS {0},{1},{2}", FileNum, FileDev, FileSec));
}
else if (PC == 0xFFBD) // SETNAM
{
StringBuilder name = new StringBuilder();
ushort addr = (ushort)(X + (Y << 8));
for (int i = 0; i < A; ++i)
name.Append((char)memory[(ushort)(addr + i)]);
System.Diagnostics.Debug.WriteLine(string.Format("SETNAM {0}", name.ToString()));
FileName = name.ToString();
}
else if (PC == 0xFFD5) // LOAD
{
FileAddr = (ushort)(X + (Y << 8));
string op;
if (A == 0)
op = "LOAD";
else if (A == 1)
op = "VERIFY";
else
op = string.Format("LOAD (A={0}) ???", A);
FileVerify = (A == 1);
ExecuteRTS();
if (A == 0 || A == 1)
{
LOAD_TRAP = PC;
// Set success
C = false;
}
else
{
SetA(14); // ILLEGAL QUANTITY message
C = true; // failure
}
return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
}
else if (PC == 0xFFD8) // SAVE
{
ushort addr1 = (ushort)(memory[A] + (memory[(ushort)(A + 1)] << 8));
ushort addr2 = (ushort)(X + (Y << 8));
System.Diagnostics.Debug.WriteLine(string.Format("SAVE {0:X4}-{1:X4}", addr1, addr2));
// Set success
C = !FileSave(FileName, addr1, addr2);
return ExecuteRTS();
}
return false;
}
protected bool ExecuteRTS()
{
byte unused_bytes;
RTS(ref PC, out unused_bytes);
return true; // return value for ExecutePatch so will reloop execution to allow berakpoint/trace/ExecutePatch/etc.
}
protected bool ExecuteJSR(ushort addr)
{
ushort retaddr = (ushort)(PC - 1);
Push(HI(retaddr));
Push(LO(retaddr));
PC = addr;
return true; // return value for ExecutePatch so will reloop execution to allow berakpoint/trace/ExecutePatch/etc.
}
// returns true if BASIC
protected bool LoadStartupPrg()
{
bool result = FileLoad(out byte unused_err);
return FileSec == 0 ? true : false; // relative is BASIC, absolute is ML
}
// returns success
protected bool FileLoad(out byte err)
{
bool startup = (StartupPRG != null);
err = 0;
ushort addr = FileAddr;
bool success = true;
try
{
string filename = startup ? StartupPRG : FileName;
if (!File.Exists(filename) && !filename.ToLower().EndsWith(".prg"))
filename += ".prg";
using (FileStream stream = File.OpenRead(filename))
{
byte lo = (byte)stream.ReadByte();
byte hi = (byte)stream.ReadByte();
if (startup)
{
if (lo == 1)
FileSec = 0;
else
FileSec = 1;
}
if (FileSec == 1) // use address in file? yes-use, no-ignore
addr = (ushort)(lo | (hi << 8)); // use address specified in file
var op = FileVerify ? "VERIFY" : "LOAD";
System.Diagnostics.Debug.WriteLine($"{op}@{addr:X4}");
int i;
while (success)
{
i = stream.ReadByte();
if (i >= 0 && i <= 255)
{
if (FileVerify)
{
if (memory[addr++] != (byte)i)
{
err = 28; // VERIFY
success = false;
}
}
else
memory[addr++] = (byte)i;
}
else
break; // end of file
}
stream.Close();
}
System.Diagnostics.Debug.WriteLine($"END {addr:X4}");
}
catch (FileNotFoundException)
{
err = 4; // FILE NOT FOUND
success = false;
}
catch (Exception)
{
err = 1; // UNKNOWN - TOO MANY FILES
success = false;
}
FileAddr = addr;
return success;
}
protected bool FileSave(string filename, ushort addr1, ushort addr2)
{
try
{
if (!filename.ToLower().EndsWith(".prg"))
filename += ".prg";
using (FileStream stream = File.OpenWrite(filename))
{
stream.WriteByte(LO(addr1));
stream.WriteByte(HI(addr1));
for (ushort addr = addr1; addr <= addr2; ++addr)
stream.WriteByte(memory[addr]);
stream.Close();
return true;
}
}
catch
{
return false;
}
}
}
}