-
Notifications
You must be signed in to change notification settings - Fork 4
/
emuc64.cs
580 lines (518 loc) · 24.2 KB
/
emuc64.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// emuc64.cs - Class EmuC64 - Commodore 64 Emulator
//
////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////
//
// This is a 6502 Emulator, designed for running Commodore 64 text mode,
// with only a few hooks: CHRIN-$FFCF/CHROUT-$FFD2/COLOR-$D021/199/646
// and implemented RAM/ROM/IO banking (BASIC could live without these)
// additional hooks added: READY/GETIN/STOP (could live without these)
// READY hook is used to load program specified on command line
// Useful as is in current state as a simple 6502 emulator
//
// LIMITATIONS:
// Only keyboard/console I/O. No text pokes, no graphics. Just stdio.
// No key scan codes (197), or keyboard buffer (198, 631-640), but INPUT S$ works
// No keyboard color switching. No border displayed. No border color.
// No screen editing (gasp!) Just short and sweet for running C64 BASIC in
// terminal/console window via 6502 chip emulation in software
// No PETSCII graphic characters, only supports printables CHR$(32) to CHR$(126),
// and CHR$(147) clear screen, home/up/down/left/right, reverse on/off
// No timers. No interrupts except BRK. No NMI/RESTORE key. ESC is STOP key.
// but TI$/TI are simulated.
//
// $00 (data direction missing)
// $01 Banking implemented (tape sense/controls missing)
// $0000-$9FFF RAM (upper limit may vary based on RAM allocated)
// $A000-$BFFF BASIC ROM
// $A000-$BFFF Banked LORAM (may not be present based on RAM allocated)
// $C000-$CFFF RAM
// $D000-$D7FF (I/O missing, reads as zeros)
// $D800-$DFFF VIC-II color RAM nybbles in I/O space (1K x 4bits)
// $D000-$DFFF Banked RAM (may not be present based on RAM allocated)
// $D000-$DFFF Banked Character ROM
// $E000-$FFFF KERNAL ROM
// $E000-$FFFF Banked HIRAM (may not be present based on RAM allocated)
//
// Requires user provided Commodore 64 BASIC/KERNAL ROMs (e.g. from VICE)
// as they are not provided, others copyrights may still be in effect.
//
////////////////////////////////////////////////////////////////////////////////
//Changing Windows console colors is known to more closely replicate the Commodore experience
//(you may vary to your preferences)
//Listed in left to right order for Console + System Menu + Properites + Colors
// 0, 0, 0 - ConsoleColor.Black
// 32, 32, 128 - ConsoleColor.DarkBlue
// 32, 96, 32 - ConsoleColor.DarkGreen
// 64, 64, 64 - ConsoleColor.DarkCyan(aka Dark Gray)
// 96, 64, 0 - ConsoleColor.DarkRed(aka Brown)
// 128, 0, 128 - ConsoleColor.DarkMagenta
// 180, 90, 0 - ConsoleColor.DarkYellow(aka Orange)
// 128, 128, 128 - ConsoleColor.Gray(aka Light Gray)
// 96, 96, 96 - ConsoleColor.DarkGray(aka Medium Gray)
// 64, 64, 255 - ConsoleColor.Blue(aka Light Blue)
// 128, 192, 128 - ConsoleColor.Green(aka Light Green)
// 0, 255, 255 - ConsoleColor.Cyan
// 128, 0, 0 - ConsoleColor.Red
// 240, 128, 128 - ConsoleColor.Magenta(aka Pink)
// 224, 224, 0 - ConsoleColor.Yellow
// 255, 255, 255 - ConsoleColor.White
//For returning to defaults, note that Windows colors default to
// 0, 0, 0 - ConsoleColor.Black
// 0, 0, 128 - ConsoleColor.DarkBlue
// 0, 128, 0 - ConsoleColor.DarkGreen
// 0, 128, 128 - ConsoleColor.DarkCyan
// 128, 0, 0 - ConsoleColor.DarkRed
// 128, 0, 128 - ConsoleColor.DarkMagenta
// 128, 128, 0 - ConsoleColor.DarkYellow
// 192, 192, 192 - ConsoleColor.Gray
// 128, 128, 128 - ConsoleColor.DarkGray
// 0, 0, 255 - ConsoleColor.Blue
// 0, 255, 0 - ConsoleColor.Green
// 0, 255, 255 - ConsoleColor.Cyan
// 255, 0, 0 - ConsoleColor.Red
// 255, 0, 255 - ConsoleColor.Magenta
// 255, 255, 0 - ConsoleColor.Yellow
// 255, 255, 255 - ConsoleColor.White
using System;
using System.IO;
using System.Text;
namespace simple_emu_c64
{
public class EmuC64 : EmuCBM
{
public EmuC64(int ram_size, string basic_file, string chargen_file, string kernal_file) : base(new C64Memory(ram_size, basic_file, chargen_file, kernal_file))
{
}
private int startup_state = 0;
private int go_state = 0;
// C64 patches
// 34/35 ($22/$23) = INDEX, temporary BASIC pointer, set before CLR
// 43/44 = start of BASIC program in RAM
// 45/46 = end of BASIC program in RAM, start of variables
// $A474 = ROM BASIC READY prompt
// $A47B = ROM BASIC MAIN, in direct mode but skip READY prompt
// $A533 = ROM LNKPRG/LINKPRG
// $A65E = CLEAR/CLR - erase variables
// $A815 = EXECUTE after parsing GO token
// $AD8A = Evaluate expression, check data type
// $B7F7 = Convert floating point to 2 byte integer Y/A
protected override bool ExecutePatch()
{
if (memory[PC] == 0x6C && memory[(ushort)(PC + 1)] == 0x30 && memory[(ushort)(PC + 2)] == 0x03) // catch JMP(LOAD_VECTOR), redirect to jump table
{
CheckBypassSETLFS();
CheckBypassSETNAM();
// note: A register has same purpose LOAD/VERIFY
X = memory[0xC3];
Y = memory[0xC4];
PC = 0xFFD5; // use KERNAL JUMP TABLE instead, so LOAD is hooked by base
return true; // re-execute
}
if (memory[PC] == 0x6C && memory[(ushort)(PC + 1)] == 0x32 && memory[(ushort)(PC + 2)] == 0x03) // catch JMP(SAVE_VECTOR), redirect to jump table
{
CheckBypassSETLFS();
CheckBypassSETNAM();
X = memory[0xAE];
Y = memory[0xAF];
A = 0xC1;
PC = 0xFFD8; // use KERNAL JUMP TABLE instead, so SAVE is hooked by base
return true; // re-execute
}
else if (PC == 0xA474 || 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]);
if (FileLoad(out byte err))
{
// set End of Program
memory[0xAE] = (byte)FileAddr;
memory[0xAF] = (byte)(FileAddr >> 8);
}
else
{
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));
is_basic = LoadStartupPrg();
// set End of Program
memory[0xAE] = (byte)FileAddr;
memory[0xAF] = (byte)(FileAddr >> 8);
}
StartupPRG = null;
if (is_basic)
{
//UNNEW that I used in late 1980s, should work well for loading a program too, probably gleaned from BASIC ROM
//listed here as reference, adapted to use in this state machine, ExecutePatch()
//ldy #0
//lda #1
//sta(43),y
//iny
//sta(43),y
//jsr $a533 ; LINKPRG
//clc
//lda $22
//adc #2
//sta 45
//lda $23
//adc #0
//sta 46
//lda #0
//jsr $a65e ; CLEAR/CLR
//jmp $a474 ; READY
// 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(0xA533); // 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(0xA65E); // CLEAR/CLR
}
else if (startup_state == 2)
{
if (PC == LOAD_TRAP)
{
X = LO(FileAddr);
Y = HI(FileAddr);
}
else
{
CBM_Console.Push("RUN\r");
PC = 0xA47B; // 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 == 0xA815) // Execute after GO
{
if (go_state == 0 && A >= (byte)'0' && A <= (byte)'9')
{
go_state = 1;
return ExecuteJSR(0xAD8A); // Evaluate expression, check data type
}
else if (go_state == 1)
{
go_state = 2;
return ExecuteJSR(0xB7F7); // Convert fp to 2 byte integer
}
else if (go_state == 2)
{
Program.go_num = (ushort)(Y + (A << 8));
exit = true;
return true;
}
}
return base.ExecutePatch();
}
private void CheckBypassSETNAM()
{
// In case caller bypassed calling SETNAM, get from lower memory
byte name_len = memory[0xB7];
ushort name_addr = (ushort)(memory[0xBB] | (memory[0xBC] << 8));
StringBuilder name = new StringBuilder();
for (byte i = 0; i < name_len; ++i)
name.Append((char)memory[(ushort)(name_addr + i)]);
if (FileName != name.ToString())
{
System.Diagnostics.Debug.WriteLine(string.Format("bypassed SETNAM {0}", name.ToString()));
FileName = name.ToString();
}
}
private void CheckBypassSETLFS()
{
// In case caller bypassed calling SETLFS, get from lower memory
if (
FileNum != memory[0xB8]
|| FileDev != memory[0xBA]
|| FileSec != memory[0xB9]
)
{
FileNum = memory[0xB8];
FileDev = memory[0xBA];
FileSec = memory[0xB9];
System.Diagnostics.Debug.WriteLine(string.Format("bypassed SETLFS {0},{1},{2}", FileNum, FileDev, FileSec));
}
}
// Commodore 64 - walk Kernal Reset vector, MAIN, CRUNCH, GONE (EXECUTE), Statements, Functions, and Operators BASIC ROM code
public override void Walk()
{
// in case cpu has not been reset, manually initialize low memory that will be called by BASIC and KERNAL ROM
for (int i = 0; i < 0x18; ++i)
memory[(ushort)(0x73 + i)] = memory[(ushort)(0xE3A2 + i)]; // CHARGET
memory[0x300] = LO(0xE38B); // ERROR
memory[0x301] = HI(0xE38B);
memory[0x302] = LO(0xA483); // MAIN
memory[0x303] = HI(0xA483);
memory[0x304] = LO(0xA57C); // CRUNCH
memory[0x305] = HI(0xA57C);
memory[0x306] = LO(0xA71A); // QPLOP
memory[0x307] = HI(0xA71A);
memory[0x308] = LO(0xA7E4); // GONE
memory[0x309] = HI(0xA7E4);
memory[0x30A] = LO(0xAE86); // EVAL
memory[0x30B] = HI(0xAE86);
memory[0x31A] = LO(0xF34A); // OPEN
memory[0x31B] = HI(0xF34A);
memory[0x31C] = LO(0xF291); // CLOSE
memory[0x31D] = HI(0xF291);
memory[0x31E] = LO(0xF20E); // CHKIN
memory[0x31F] = HI(0xF20E);
memory[0x320] = LO(0xF250); // CKOUT
memory[0x321] = HI(0xF250);
memory[0x322] = LO(0xF333); // CHECK STOP
memory[0x323] = HI(0xF333);
memory[0x324] = LO(0xF157); // CHRIN
memory[0x325] = HI(0xF157);
memory[0x326] = LO(0xF1CA); // CHROUT
memory[0x327] = HI(0xF1CA);
memory[0x328] = LO(0xF6ED); // STOP
memory[0x329] = HI(0xF6ED);
memory[0x32A] = LO(0xF13E); // GETIN
memory[0x32B] = HI(0xF13E);
memory[0x32C] = LO(0xF32F); // CLALL
memory[0x32D] = HI(0xF32F);
memory[0x330] = LO(0xF4A5); // LOAD
memory[0x331] = HI(0xF4A5);
memory[0x332] = LO(0xF5ED); // SAVE
memory[0x333] = HI(0xF5ED);
base.Walk(); // reset seen, and walk the RESET vector
ushort addr;
addr = (ushort)(memory[0xFFFE] | (memory[0xFFFF] << 8)); // IRQ
Walk6502.Walk(this, addr);
addr = (ushort)(memory[0xFFFA] | (memory[0xFFFB] << 8)); // NMI
Walk6502.Walk(this, addr);
// Portion of MAIN, CRUNCH and GONE(Execute) or MAIN1(Store line)
Walk6502.Walk(this, 0xA494);
for (ushort table = 0xA00C; table < 0xA051; table += 2) // BASIC Statements
{
addr = (ushort)((memory[table] | (memory[(ushort)(table + 1)] << 8)) + 1); // put on stack for RTS, so must add one
Walk6502.Walk(this, addr);
}
for (ushort table = 0xA052; table < 0xA07F; table += 2) // Function Dispatch
{
addr = (ushort)((memory[table] | (memory[(ushort)(table + 1)] << 8)));
Walk6502.Walk(this, addr);
}
for (ushort table = 0xA080; table < 0xA09D; table += 3) // Operator Dispatch
{
addr = (ushort)((memory[(ushort)(table + 1)] | (memory[(ushort)(table + 2)] << 8)) + 1); // put on stack for RTS, so must add one
Walk6502.Walk(this, addr);
}
}
///////////////////////////////////////////////////////////////////////
class C64Memory : Emu6502.Memory
{
byte[] ram;
byte[] basic_rom;
byte[] char_rom;
byte[] kernal_rom;
byte[] io;
// note ram starts at 0x0000
const int basic_addr = 0xA000;
const int kernal_addr = 0xE000;
const int io_addr = 0xD000;
const int io_size = 0x1000;
const int color_addr = 0xD800;
const int color_size = 0x0400;
const int open_addr = 0xC000;
const int open_size = 0x1000;
public void ApplyColor()
{
CBM_Console.Reverse = (this[199] != 0);
if (CBM_Console.Color)
{
if (CBM_Console.Reverse && CBM_Console.Encoding != CBM_Console.CBMEncoding.petscii)
{
Console.BackgroundColor = ToConsoleColor(this[646]);
Console.ForegroundColor = ToConsoleColor(this[0xD021]);
}
else
{
Console.ForegroundColor = ToConsoleColor(this[646]);
Console.BackgroundColor = ToConsoleColor(this[0xD021]);
}
}
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;
}
}
}
private ConsoleColor ToConsoleColor(byte CommodoreColor)
{
switch (CommodoreColor & 0xF)
{
case 0: return ConsoleColor.Black;
case 1: return ConsoleColor.White;
case 2: return ConsoleColor.Red;
case 3: return ConsoleColor.Cyan;
case 4: return ConsoleColor.DarkMagenta;
case 5: return ConsoleColor.DarkGreen;
case 6: return ConsoleColor.DarkBlue;
case 7: return ConsoleColor.Yellow;
case 8: return ConsoleColor.DarkYellow;//Orange
case 9: return ConsoleColor.DarkRed;//Brown
case 10: return ConsoleColor.Magenta;//Pink
case 11: return ConsoleColor.DarkCyan;//Dark Gray
case 12: return ConsoleColor.DarkGray;//Medium Gray
case 13: return ConsoleColor.Green;//Light Green
case 14: return ConsoleColor.Blue;//Light Blue
case 15: return ConsoleColor.Gray;//Light Gray
default: throw new InvalidOperationException("Missing case number in ToConsoleColor");
}
}
public C64Memory(int ram_size, string basic_file, string chargen_file, string kernal_file)
{
if (ram_size > 64 * 1024)
ram_size = 64 * 1024;
ram = new byte[ram_size];
basic_rom = File.ReadAllBytes(basic_file);
char_rom = File.ReadAllBytes(chargen_file);
kernal_rom = File.ReadAllBytes(kernal_file);
for (int i = 0; i < ram.Length; ++i)
ram[i] = 0;
io = new byte[io_size];
for (int i = 0; i < io.Length; ++i)
io[i] = 0;
// initialize DDR and memory mapping to defaults
ram[0] = 0xEF;
ram[1] = 0x07;
}
public byte this[ushort addr]
{
get
{
if (addr <= ram.Length - 1 // note: handles option to have less than 64K RAM
&& (
addr < basic_addr // always RAM
|| (addr >= open_addr && addr < open_addr + open_size) // always open RAM C000.CFFF
|| (((ram[1] & 3) != 3) && addr >= basic_addr && addr < basic_addr + basic_rom.Length) // RAM banked instead of BASIC
|| (((ram[1] & 2) == 0) && addr >= kernal_addr && addr <= kernal_addr + kernal_rom.Length - 1) // RAM banked instead of KERNAL
|| (((ram[1] & 3) == 0) && addr >= io_addr && addr < io_addr + io.Length) // RAM banked instead of IO
)
)
return ram[addr];
else if (addr >= basic_addr && addr < basic_addr + basic_rom.Length)
return basic_rom[addr - basic_addr];
else if (addr >= io_addr && addr < io_addr + io.Length)
{
if ((ram[1] & 4) == 0)
return char_rom[addr - io_addr];
else if (addr >= color_addr && addr < color_addr + color_size)
return (byte)(io[addr - io_addr] | 0xF0); // set high bits to show this is a nybble
else
return io[addr - io_addr];
}
else if (addr >= kernal_addr && addr <= kernal_addr + kernal_rom.Length - 1)
return kernal_rom[addr - kernal_addr];
else
return 0xFF;
}
set
{
if (addr <= ram.Length - 1 // note: handles option to have less than 64K RAM
&& (
addr < io_addr // RAM, including open RAM, and RAM under BASIC
|| (addr >= kernal_addr && addr <= kernal_addr + kernal_rom.Length - 1) // RAM under KERNAL
|| (((ram[1] & 7) == 0) && addr >= io_addr && addr < io_addr + io.Length) // RAM banked in instead of IO
)
)
{
ram[addr] = value; // banked RAM, and RAM under ROM
if (addr == 646 || addr == 199)
ApplyColor();
else if (addr == 216)
CBM_Console.InsertMode = (value != 0);
else if (addr == 212)
CBM_Console.QuoteMode = (value != 0);
}
else if (addr == 0xD021) // background
{
io[addr - io_addr] = (byte)(value & 0xF); // store value so can be retrieved
ApplyColor();
}
else if (addr >= color_addr && addr < color_addr + color_size)
io[addr - io_addr] = value;
else if (addr == 0xD018) // bit 2 is upper(unset)/lower(set)
{
io[addr - io_addr] = value;
CBM_Console.Lowercase = (value & 2) != 0;
}
//else if (addr >= io_addr && addr < io_addr + io.Length)
// io[addr - io_addr] = value;
}
}
}
}
}