-
Notifications
You must be signed in to change notification settings - Fork 4
/
emuted.cs
420 lines (390 loc) · 17 KB
/
emuted.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// emuted.cs - Class EmuTED - Commodore TED Emulator (C16, Plus/4, etc.)
//
////////////////////////////////////////////////////////////////////////////////
//
// simple-emu-c64
// C64/6502 Emulator for Microsoft Windows Console
//
// MIT License
//
// Copyright (c) 2022 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.
//
////////////////////////////////////////////////////////////////////////////////
//
// This is a 6502 Emulator, designed for running Commodore computers in text mode,
// with only a few hooks: CHRIN-$FFCF/CHROUT-$FFD2, and minimal I/O to function
// Useful as is in current state as a simple 6502 emulator and BASIC console
//
// LIMITATIONS: See EmuC64
//
// MEMORY MAP:
// $0000-$3FFF 16K RAM, note repeats $4000-$7FFF, $8000-$BFFF, $C000-$FFFF
// ($0000-$7FFF 32K RAM, note repeats $8000-$FFFF)
// ($0000-$FFFF 64K RAM minus non-banked portions, upper RAM can be banked to ROM)
// $8000-$BFFF BASIC ROM
// $FC00-$FCFF NON-BANKED KERNAL ROM (but can be banked to RAM)
// $FD00-$FF3F I/O Registers (never banked, not even to RAM)
// $FDD0-$FDDF ROM config addresses (A1/A0: BASIC/FUNCT/CART/RESV, A3/A2: KERNAL/FUNCT/CART/RESV)
// $FF3E write banks to ROM
// $FF3F write banks to RAM
// $C000-$FFFF KERNAL ROM (minus I/O, can be banked to RAM)
//
// Requires user provided Commodore 16 BASIC/KERNAL ROMs (e.g. from VICE)
// as they are not provided, others copyrights may still be in effect.
//
// ROM has easter egg for credits
// SYS 52650
//
// Function ROMs not implemented
// (because not supporting screen editing and key scan codes yet)
// but this looks interesting for future reference:
// https://www.rift.dk/commodore-16-internal-function-rom/
//
//
// Credits to
// Commodore Source https://github.com/mist64/cbmsrc
// C16 and Plus/4 Memory Map https://www.floodgap.com/retrobits/ckb/secret/264memory.txt
////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
namespace simple_emu_c64
{
public class EmuTED : EmuCBM
{
public EmuTED(int ram_size, string basic_file, string kernal_file) : base(new TEDMemory(ram_size, basic_file, kernal_file))
{
if (!CBM_Console.Color)
{
Console.BackgroundColor = startup_bg;
Console.ForegroundColor = startup_fg;
}
}
private int startup_state = 0;
private int go_state = 0;
protected override bool ExecutePatch()
{
if (PC == 0x8703 || PC == LOAD_TRAP) // READY
{
go_state = 0;
if (startup_state == 0 && (StartupPRG != null || PC == LOAD_TRAP))
{
bool is_basic;
if (PC == LOAD_TRAP)
{
is_basic = (
FileVerify == false
&& FileSec == 0 // relative load, not absolute
&& LO(FileAddr) == memory[43] // requested load address matches BASIC start
&& HI(FileAddr) == memory[44]);
memory[0xFF3F] = 0; // switch to RAM
bool success = FileLoad(out byte err);
memory[0xFF3E] = 0; // switch to ROM
if (!success)
{
System.Diagnostics.Debug.WriteLine(string.Format("FileLoad() failed: err={0}, file {1}", err, StartupPRG));
C = true; // signal error
SetA(err); // FILE NOT FOUND or VERIFY
// so doesn't repeat
StartupPRG = null;
LOAD_TRAP = -1;
return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
}
}
else
{
FileName = StartupPRG;
FileAddr = (ushort)(memory[43] | (memory[44] << 8));
memory[0xFF3F] = 0; // switch to RAM
is_basic = LoadStartupPrg();
memory[0xFF3E] = 0; // switch to ROM
}
StartupPRG = null;
if (is_basic)
{
// initialize first couple bytes (may only be necessary for UNNEW?)
ushort addr = (ushort)(memory[43] | (memory[44] << 8));
memory[addr] = 1;
memory[(ushort)(addr + 1)] = 1;
startup_state = 1; // should be able to regain control when returns...
return ExecuteJSR(0x8818); // LINKPRG
}
else
{
LOAD_TRAP = -1;
X = LO(FileAddr);
Y = HI(FileAddr);
C = false;
}
}
else if (startup_state == 1)
{
ushort addr = (ushort)(memory[0x22] | (memory[0x23] << 8) + 2);
memory[45] = (byte)addr;
memory[46] = (byte)(addr >> 8);
SetA(0);
startup_state = 2; // should be able to regain control when returns...
return ExecuteJSR(0x8A98); // CLEAR/CLR
}
else if (startup_state == 2)
{
if (PC == LOAD_TRAP)
{
X = LO(FileAddr);
Y = HI(FileAddr);
}
else
{
CBM_Console.Push("RUN\r");
PC = 0x8706; // skip READY message, but still set direct mode, and continue to MAIN
}
C = false; // signal success
startup_state = 0;
LOAD_TRAP = -1;
return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
}
}
else if (PC == 0x8C77) // Execute after GO
{
if (go_state == 0 && A >= (byte)'0' && A <= (byte)'9')
{
go_state = 1;
return ExecuteJSR(0x8E3E); // Get integer into $14/$15
}
else if (go_state == 1)
{
Program.go_num = (ushort)(memory[0x14] + (memory[0x15] << 8));
exit = true;
return true;
}
else
go_state = 0;
}
else if (PC == 0xFFBD) // SETNAM
{
memory[0xFF3F] = 0; // switch to RAM
bool retvalue = base.ExecutePatch();
memory[0xFF3E] = 0; // switch to ROM
return retvalue;
}
return base.ExecutePatch();
}
class TEDMemory : Emu6502.Memory
{
byte[] ram; // note if less than 64K, then addressing wraps around
byte[] basic_rom;
byte[] kernal_rom;
byte[] io;
bool rom_enabled = true; // FF3E=rom & FF3F=ram
// note ram starts at 0x0000
const int basic_addr = 0x8000;
const int kernal_addr = 0xC000;
const int io_addr = 0xFD00; // IO cannot be banked ever
const int io_length = 0x0240; // note hole in IO: FF20-FF3D, RAM or ROM?
const int nonbank_kernal = 0xFC00; // can be banked to RAM, but not to other ROMs
const int nonbank_len = 0x0100;
const int config_addr = 0xFDD0;
const int config_len = 0x0010;
int rom_config = 0;
// ROM configuration in I/O
// A1/A0: 00=BASIC LO, 01=FUNCTION LO, 10=CARTRIDGE LO, 11=RESERVED LO
// A3/A2: 00=KERNAL HI, 01=FUNCTION HI, 10=CARTRIDGE HI, 11=RESERVED HI
// FDD0 BASIC LO/KERNAL HI
// FDD1 FUNCTION LO/KERNAL HI
// FDD2 CARTRIDGE LO/KERNAL HI
// FDD3 RESERVED LO/KERNAL HI
// FDD4 BASIC LO/FUNCTION HI
// FDD5 FUNCTION LO/FUNCTION HI
// FDD6 CARTRIDGE LO/FUNCTION HI
// FDD7 RESERVED LO/FUNCTION HI
// FDD8 BASIC LO/CARTRIDGE HI
// FDD9 FUNCTION LO/CARTRIDGE HI
// FDDA CARTRIDGE LO/CARTRIDGE HI
// FDDB RESERVED/CARTRIDGE HI
// FDDC BASIC LO/RESERVED HI
// FDDD FUNCTION LO/RESERVED HI
// FDDE CARTRIDGE LO/RESERVED HI
// FDDF RESERVED LO/RESERVED HI
public TEDMemory(int ram_size, string basic_file, string kernal_file)
{
if (ram_size < 32 * 1024)
ram_size = 16 * 1024;
else if (ram_size < 64 * 1024)
ram_size = 32 * 1024;
else
ram_size = 64 * 1024;
ram = new byte[ram_size];
basic_rom = File.ReadAllBytes(basic_file);
kernal_rom = File.ReadAllBytes(kernal_file);
for (int i = 0; i < ram.Length; ++i)
ram[i] = 0;
io = new byte[io_length];
for (int i = 0; i < io.Length; ++i)
io[i] = 0;
}
public byte this[ushort addr]
{
get
{
if (addr == 0xFF08) // key matrix
return 0xFF; // FF = no key, 7F = stop (open in monitor)
if (addr >= io_addr && addr < io_addr + io.Length)
return io[addr - io_addr];
else if (!rom_enabled || addr < basic_addr)
return ram[addr & (ram.Length - 1)]; // note RAM wraps around when less than 64K
else if (((rom_config & 0x03) == 0) && rom_enabled && addr >= basic_addr && addr < basic_addr + basic_rom.Length)
return basic_rom[addr - basic_addr];
else if (((rom_config & 0x0C) == 0) && rom_enabled && (addr >= kernal_addr && addr < kernal_addr + kernal_rom.Length) || (addr >= nonbank_kernal && addr < nonbank_kernal + nonbank_len))
return kernal_rom[addr - kernal_addr];
else
return 0xFF;
}
set
{
if (addr == 0xFF3E)
rom_enabled = true;
else if (addr == 0xFF3F)
rom_enabled = false;
else if (addr >= config_addr && addr < config_addr + config_len)
rom_config = addr & 0xF;
else if (addr >= io_addr && addr < io_addr + io.Length)
{
io[addr - io_addr] = value;
if (addr == 65301)
ApplyColor();
else if (addr == 0xFF13)
CBM_Console.Lowercase = (value & 4) != 0;
}
else
{
ram[addr & (ram.Length - 1)] = value; // includes writing under rom, note RAM wraps around when less than 64K
if (addr == 194 || addr == 1339)
ApplyColor();
else if (addr == 207)
CBM_Console.InsertMode = (value != 0);
else if (addr == 203)
CBM_Console.QuoteMode = (value != 0);
}
}
}
private void ApplyColor()
{
CBM_Console.Reverse = (this[194] != 0);
if (CBM_Console.Color)
{
var bg_register = io[65301-io_addr];
var bg_color = (TedColor)(bg_register & 0xF);
var bg_luminance = (bg_register >> 4) & 7;
var fg_color = (TedColor)(ram[1339] & 0xF);
var fg_luminance = (ram[1339] >> 4) & 7;
if (CBM_Console.Reverse && CBM_Console.Encoding != CBM_Console.CBMEncoding.petscii)
{
Console.BackgroundColor = ConvertToConsoleColor(fg_color, fg_luminance);
Console.ForegroundColor = ConvertToConsoleColor(bg_color, bg_luminance);
}
else
{
Console.BackgroundColor = ConvertToConsoleColor(bg_color, bg_luminance);
Console.ForegroundColor = ConvertToConsoleColor(fg_color, fg_luminance);
}
}
else
{
if (CBM_Console.Reverse && CBM_Console.Encoding != CBM_Console.CBMEncoding.petscii)
{
Console.BackgroundColor = startup_fg;
Console.ForegroundColor = startup_bg;
}
else
{
Console.ForegroundColor = startup_fg;
Console.BackgroundColor = startup_bg;
}
}
}
enum TedColor
{
Black = 0,
Gray = 1,
White = 1,
Red = 2,
Cyan = 3,
Magenta = 4,
Green = 5,
Blue = 6,
Yellow = 7,
Orange = 8,
Brown = 9,
YellowGreen = 10,
Pink = 11,
BlueGreen = 12,
LightBlue = 13,
Purple = 14,
LightGreen = 15
}
ConsoleColor ConvertToConsoleColor(TedColor color, int luminance)
{
switch (color)
{
case TedColor.Black:
return ConsoleColor.Black;
case TedColor.Gray:
if (luminance < 2)
return ConsoleColor.DarkCyan;
else if (luminance < 4)
return ConsoleColor.DarkGray;
else if (luminance < 7)
return ConsoleColor.Gray;
else
return ConsoleColor.White;
case TedColor.Red:
return ConsoleColor.Red;
case TedColor.Cyan:
return ConsoleColor.Cyan;
case TedColor.Magenta:
return ConsoleColor.DarkMagenta;
case TedColor.Green:
return ConsoleColor.DarkGreen;
case TedColor.Blue:
return ConsoleColor.DarkBlue;
case TedColor.Yellow:
return ConsoleColor.Yellow;
case TedColor.Orange:
return ConsoleColor.DarkYellow;
case TedColor.Brown:
return ConsoleColor.DarkRed;
case TedColor.YellowGreen:
return ConsoleColor.Green;
case TedColor.Pink:
return ConsoleColor.Magenta;
case TedColor.BlueGreen:
return ConsoleColor.DarkGreen;
case TedColor.LightBlue:
return ConsoleColor.Blue;
case TedColor.Purple:
return ConsoleColor.DarkMagenta;
case TedColor.LightGreen:
return ConsoleColor.Green;
default:
return ConsoleColor.Black;
}
}
}
}
}