-
Notifications
You must be signed in to change notification settings - Fork 4
/
cbmconsole.cs
336 lines (309 loc) · 11.7 KB
/
cbmconsole.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
// cbmconsole.cs - Class CBM_Console - Commodore Console Emulation
//
////////////////////////////////////////////////////////////////////////////////
//
// 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;
namespace simple_emu_c64
{
public class CBM_Console
{
static bool supress_next_clear = true;
static bool supress_next_home = false;
static bool supress_next_cr = false;
static List<char> buffer = new List<char>();
static List<ConsoleKeyInfo> keybuffer = new List<ConsoleKeyInfo>();
static bool stop_pressed = false;
public enum CBMEncoding
{
ascii = 0,
//ansi = 1,
//unicode = 2,
petscii = 3,
}
private static CBMEncoding encoding;
public static CBMEncoding Encoding
{
get
{
return encoding;
}
set
{
encoding = value;
if (encoding == CBMEncoding.ascii)
Console.OutputEncoding = System.Text.Encoding.ASCII;
else if (encoding == CBMEncoding.petscii)
Console.OutputEncoding = System.Text.Encoding.UTF8;
}
}
public static bool Color { get; set; }
//Warning: Default light blue on blue doesn't look good with default color palette for Windows console
//You can manually modify color pallette in Windows (see suggested colors documented in emuc64.cs)
public static bool Lowercase { get; set; }
public static bool Reverse { get; set; }
public static bool QuoteMode { get; set; }
public static bool InsertMode { get; set; }
public static void WriteChar(char c, bool supress_next_home=false)
{
// we're emulating, so draw character on local console window
if (c == 0x0D || c == 0x8D)
{
if (supress_next_cr)
supress_next_cr = false;
else
Console.WriteLine();
}
else if (encoding == CBMEncoding.petscii && (byte)c < 32 && (QuoteMode || InsertMode) && !((byte)c == 20 && !InsertMode))
{
Console.Write((char)(0xE200 | ((byte)c + (byte)'A' - 1) | (Lowercase ? 0x100 : 0)));
return;
}
else if (encoding == CBMEncoding.petscii && (byte)c >= 128 && ((byte)c & 127) < 32 && (QuoteMode || InsertMode))
{
Console.Write((char)(0xE200 | (((byte)c & 127) + (byte)'a' - 1) | (Lowercase ? 0x100 : 0)));
return;
}
else if (encoding == CBMEncoding.petscii && ((byte)c & 127) >= 32)
{
if (Reverse)
{
Console.Write((char)(0xE200 | (byte)c | (Lowercase ? 0x100 : 0)));
return;
}
if (c == '\\')
{
Console.Write('£');
return;
}
else if (c == '^')
{
Console.Write('↑');
return;
}
else if (c == '_')
{
Console.Write('←');
return;
}
else if (c == '\x7E')
{
Console.Write('π');
return;
}
else if (c >= '\x20' && c < '\x40' || c == '[' || c == ']')
{
Console.Write(c);
return;
}
else if (c >= 'A' && c <= 'Z' || Lowercase && c >= 'a' && c <= 'z')
{
if (Lowercase)
Console.Write((char)((int)c ^ 0x20));
else
Console.Write(c);
return;
}
Console.Write((char)(0xE000 | (byte)c | (Lowercase ? 0x100 : 0)));
}
else if (c >= ' ' && c <= '~')
Console.Write(c);
else if (c == 157) // left
{
if (Console.CursorLeft > 0)
Console.Write('\b');
else if (Console.CursorTop > 0)
{
--Console.CursorTop;
Console.CursorLeft = Console.BufferWidth - 1;
}
}
else if (c == 29) // right
{
if (Console.CursorLeft < Console.BufferWidth - 1)
++Console.CursorLeft;
else
Console.WriteLine();
}
else if (c == 145) // up
{
if (Console.CursorTop > 0)
--Console.CursorTop;
}
else if (c == 17) // down
{
int left = Console.CursorLeft;
Console.WriteLine();
Console.CursorLeft = left;
}
else if (c == 19) // home
{
if (CBM_Console.supress_next_home) // use class static here, if arg set, will set for next time
{
CBM_Console.supress_next_home = false;
}
else
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
}
}
else if (c == 147)
{
try
{
if (supress_next_clear)
supress_next_clear = false;
else
Console.Clear();
}
catch (Exception)
{
// ignore exception, e.g. not a console
}
}
if (supress_next_home)
CBM_Console.supress_next_home = true;
}
// blocking read to get next typed character
public static byte ReadChar()
{
if (buffer.Count == 0)
{
// System.Console.ReadLine() has features of history (cursor up/down, F7/F8), editing (cursor left/right, delete, backspace, etc.)
buffer.AddRange(Console.ReadLine());
buffer.Add('\r');
//Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1); // Up one line
supress_next_cr = true;
}
char c = buffer[0];
buffer.RemoveAt(0);
if (Lowercase && (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'))
c = (char)((byte)c ^ 0x20); // toggle case
else if (c == '\b')
c = '\x14';
else if (encoding == CBMEncoding.petscii)
{
if (c == 'π')
c = '\xff';
else if (c == '£')
c = '\\';
else if (c == '↑' || c == '\u0018')
c = '^';
else if (c == '←' || c == '\u001b')
c = '_';
else if (c == '\u00A0') // no-break space
c = '\xA0'; // alternate space
else if (c == '\u250c' || c == '\u250f')
c = '\xB0'; // nw box line corner
else if (c == '\u2510' || c == '\u2513')
c = '\xAE'; // ne box line corner
else if (c == '\u2514' || c == '\u2517')
c = '\xAD'; // sw box line corner
else if (c == '\u2518' || c == '\u251b')
c = '\xBD'; // se box line corner
else if (c == '\u2022')
c = '\xD1'; // bullet
else if (c >= '\ue000' && c <= '\ue0ff')
c = (char)(byte)c;
//else if (c > '\xff')
// System.Diagnostics.Debug.WriteLine($"Received unicode {(ulong)c}");
}
return (byte)c;
}
public static byte GetIn(bool check_stop = false)
{
if (buffer.Count == 0 || check_stop)
{
if (!Console.KeyAvailable && !check_stop && keybuffer.Count == 0)
return 0;
ConsoleKeyInfo key;
if (check_stop || keybuffer.Count == 0)
{
key = Console.ReadKey(true);
if (check_stop)
keybuffer.Add(key); // assume must be buffered
}
else
{
key = keybuffer[0];
keybuffer.RemoveAt(0);
}
//System.Diagnostics.Debug.WriteLine($"{key.Key} {key.Modifiers} {(int)key.KeyChar}");
if (key.Key == ConsoleKey.LeftArrow)
return 157;
if (key.Key == ConsoleKey.RightArrow)
return 29;
if (key.Key == ConsoleKey.UpArrow)
return 145;
if (key.Key == ConsoleKey.DownArrow)
return 17;
if (key.Key == ConsoleKey.Home)
{
if ((key.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
return 147;
else
return 19;
}
if (key.Key == ConsoleKey.Delete)
return 20;
if (key.KeyChar == 0)
return 0;
if (key.KeyChar == 27)
{
if (check_stop)
keybuffer.RemoveAt(keybuffer.Count - 1); // don't buffer STOP
stop_pressed = true;
return 0;
}
if (check_stop)
keybuffer.RemoveAt(keybuffer.Count - 1); // not buffered here
Push(key.KeyChar.ToString());
if (check_stop)
return 0;
}
return ReadChar();
}
public static bool CheckStop()
{
if (Console.KeyAvailable)
CBM_Console.GetIn(check_stop: true);
if (stop_pressed)
{
stop_pressed = false;
buffer.Clear();
return true;
}
return false;
}
public static void Push(string s)
{
buffer.AddRange(s);
}
}
}