-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfdisk_screen.c
349 lines (297 loc) · 8.89 KB
/
fdisk_screen.c
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
#include "fdisk_screen.h"
#ifdef __CC65__
#include "ascii.h"
#include <memory.h>
#endif
#ifndef __CC65__
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#endif
extern unsigned char *charset;
long screen_line_address = SCREEN_ADDRESS;
char screen_column = 0;
#ifdef __CC65__
unsigned char *footer_messages[FOOTER_MAX + 1] = {
" MEGA65 FDISK V0.35 (C) 2017-2024 MEGA - MUSEUM OF ELECTRONIC GAMES & ART ",
" ",
" A FATAL ERROR HAS OCCURRED, SORRY. "
};
unsigned char screen_hex_buffer[6];
unsigned char to_screen_hex(unsigned char c)
{
c &= 0xf;
if (c > 9)
return 0x37 + c;
return 0x30 + c;
}
void screen_hex_byte(unsigned int addr, long value)
{
POKE(addr + 0, to_screen_hex(value >> 4));
POKE(addr + 1, to_screen_hex(value >> 0));
}
void screen_hex(unsigned int addr, long value)
{
POKE(addr + 0, to_screen_hex(value >> 28));
POKE(addr + 1, to_screen_hex(value >> 24));
POKE(addr + 2, to_screen_hex(value >> 20));
POKE(addr + 3, to_screen_hex(value >> 16));
POKE(addr + 4, to_screen_hex(value >> 12));
POKE(addr + 5, to_screen_hex(value >> 8));
POKE(addr + 6, to_screen_hex(value >> 4));
POKE(addr + 7, to_screen_hex(value >> 0));
}
void format_hex(const unsigned int addr, const long value, const char columns)
{
char i, c;
char dec[9];
screen_hex((int)&dec[0], value);
c = 8 - columns;
while (c) {
for (i = 0; i < 7; i++)
dec[i] = dec[i + 1];
dec[7] = ' ';
c--;
}
for (i = 0; i < columns; i++)
lpoke(addr + i, dec[i]);
}
#endif
unsigned char screen_decimal_digits[16][5] = { { 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 2 }, { 0, 0, 0, 0, 4 }, { 0, 0, 0, 0, 8 },
{ 0, 0, 0, 1, 6 }, { 0, 0, 0, 3, 2 }, { 0, 0, 0, 6, 4 }, { 0, 0, 1, 2, 8 }, { 0, 0, 2, 5, 6 }, { 0, 0, 5, 1, 2 },
{ 0, 1, 0, 2, 4 }, { 0, 2, 0, 4, 8 }, { 0, 4, 0, 9, 6 }, { 0, 8, 1, 9, 2 }, { 1, 6, 3, 8, 4 }, { 3, 2, 7, 6, 8 } };
void write_line(char *s, char col)
{
#ifdef __CC65__
char len = 0;
while (s[len])
len++;
if (len)
lcopy((long)&s[0], screen_line_address + col, len);
screen_line_address += 80;
if ((screen_line_address - SCREEN_ADDRESS) >= (24 * 80)) {
screen_line_address -= 80;
lcopy(SCREEN_ADDRESS + 80, SCREEN_ADDRESS, 23 * 80);
lcopy(COLOUR_RAM_ADDRESS + 80, COLOUR_RAM_ADDRESS, 23 * 80);
lfill(SCREEN_ADDRESS + 23 * 80, ' ', 80);
lfill(COLOUR_RAM_ADDRESS + 23 * 80, 1, 80);
}
#else
for (int i = 0; i < col; i++)
fprintf(stdout, " ");
fprintf(stdout, "%s\n", s);
#endif
}
void format_decimal(const unsigned int addr, const int value, const char columns)
{
#ifdef __CC65__
char i;
char dec[6];
screen_decimal((int)&dec[0], value);
for (i = 0; i < columns; i++)
lpoke(addr + i, dec[i]);
#else
printf("%08X", value);
#endif
}
void recolour_last_line(char colour)
{
#ifdef __CC65__
long colour_address = COLOUR_RAM_ADDRESS + (screen_line_address - SCREEN_ADDRESS) - 80;
lfill(colour_address, colour, 80);
#endif
return;
}
#ifdef __CC65__
unsigned char ii, j, carry, temp;
unsigned int value;
void screen_decimal(unsigned int addr, unsigned int v)
{
value = v;
// Start with all zeros
for (ii = 0; ii < 5; ii++)
screen_hex_buffer[ii] = 0;
// Add power of two strings for all non-zero bits in value.
// XXX - We should use BCD mode to do this more efficiently
for (ii = 0; ii < 16; ii++) {
if (value & 1) {
carry = 0;
for (j = 4; j < 128; j--) {
temp = screen_hex_buffer[j] + screen_decimal_digits[ii][j] + carry;
if (temp > 9) {
temp -= 10;
carry = 1;
}
else
carry = 0;
screen_hex_buffer[j] = temp;
}
}
value = value >> 1;
}
// Now convert to ascii digits
for (j = 0; j < 5; j++)
screen_hex_buffer[j] = screen_hex_buffer[j] | '0';
// and shift out leading zeros
for (j = 0; j < 4; j++) {
if (screen_hex_buffer[0] != '0')
break;
screen_hex_buffer[0] = screen_hex_buffer[1];
screen_hex_buffer[1] = screen_hex_buffer[2];
screen_hex_buffer[2] = screen_hex_buffer[3];
screen_hex_buffer[3] = screen_hex_buffer[4];
screen_hex_buffer[4] = ' ';
}
// Copy to screen
for (j = 0; j < 5; j++)
POKE(addr + j, screen_hex_buffer[j]);
}
long addr;
void display_footer(unsigned char index)
{
addr = (long)footer_messages[index];
lcopy(addr, FOOTER_ADDRESS, 80);
set_screen_attributes(FOOTER_ADDRESS, 80, ATTRIB_REVERSE);
}
#endif
#ifndef __CC65__
void setup_screen(void)
{
}
#else
void setup_screen(void)
{
unsigned char v;
mega65_io_enable();
// 80-column mode, fast CPU, extended attributes enable
*((unsigned char *)0xD031) = 0xe0;
// Put screen memory somewhere (2KB required)
// We are using $8000-$87FF for screen
// Using custom charset @ $A000
*(unsigned char *)0xD018U = (((CHARSET_ADDRESS - 0x8000U) >> 11) << 1) + (((SCREEN_ADDRESS - 0x8000U) >> 10) << 4);
// VIC RAM Bank to $8000-$BFFF
v = *(unsigned char *)0xDD00U;
v &= 0xfc;
v |= 0x01;
*(unsigned char *)0xDD00U = v;
v = *(unsigned char *)0xDD02U;
v |= 0x03;
*(unsigned char *)0xDD02U = v;
// Screen colours
POKE(0xD020U, 0);
POKE(0xD021U, 6);
// Shove screen right one pixel, as the C65 BASIC does in 80 column mode
// XXX For some reason in this mode its still one pixel out, so move it two
POKE(0xD016, 0xC9);
// Clear screen RAM
lfill(SCREEN_ADDRESS, 0x20, 2000);
// Clear colour RAM: white text
lfill(0x1f800, 0x01, 2000);
// Copy ASCII charset into place
lcopy((int)&charset[0], CHARSET_ADDRESS, 0x800);
// Set screen line address and write point
screen_line_address = SCREEN_ADDRESS;
screen_column = 0;
display_footer(FOOTER_COPYRIGHT);
}
void screen_colour_line(unsigned char line, unsigned char colour)
{
// Set colour RAM for this screen line to this colour
// (use bit-shifting as fast alternative to multiply)
lfill(0x1f800 + (line << 6) + (line << 4), colour, 80);
}
#endif
static unsigned char fatal_i;
#ifdef __CC65__
void fatal_error(unsigned char *filename, unsigned int line_number)
{
display_footer(FOOTER_FATAL);
for (fatal_i = 0; filename[fatal_i]; fatal_i++)
POKE(FOOTER_ADDRESS + 44 + fatal_i, filename[fatal_i]);
POKE(FOOTER_ADDRESS + 44 + fatal_i, ':');
fatal_i++;
screen_decimal(FOOTER_ADDRESS + 44 + fatal_i, line_number);
lfill(COLOUR_RAM_ADDRESS - SCREEN_ADDRESS + FOOTER_ADDRESS, 2 | ATTRIB_REVERSE, 80);
for (;;)
continue;
}
#else
void fatal_error(unsigned char *filename, unsigned int line_number)
{
fatal_i = 0;
fprintf(stderr, "%s:%d: Fatal error encountered.\n", filename, line_number);
exit(-1);
}
#endif
#ifdef __CC65__
void set_screen_attributes(long p, unsigned char count, unsigned char attr)
{
// This involves setting colour RAM values, so we need to either LPOKE, or
// map the 2KB colour RAM in at $D800 and work with it there.
// XXX - For now we are LPOKING
long addr = COLOUR_RAM_ADDRESS - SCREEN_ADDRESS + p;
for (fatal_i = 0; fatal_i < count; fatal_i++) {
lpoke(addr, lpeek(addr) | attr);
addr++;
}
}
char read_line(char *buffer, unsigned char maxlen)
{
char len = 0;
char c;
char reverse = 0x90;
// Read input using hardware keyboard scanner
// Flush keyboard input queue before reading input
while (PEEK(0xD610))
POKE(0xD610, 0);
while (len < maxlen) {
c = *(unsigned char *)0xD610;
#if 0
reverse ^=0x20;
#endif
// Show cursor
lpoke(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS,
reverse | (lpeek(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS) & 0xf));
if (c) {
if (c == 0x14) {
// DELETE
if (len) {
// Remove blink attribute from this char
lpoke(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS,
lpeek(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS) & 0xf);
// Go back one and erase
len--;
lpoke(screen_line_address + len, ' ');
// Re-enable blink for cursor
lpoke(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS,
lpeek(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS) | reverse);
buffer[len] = 0;
}
}
else if (c == 0x0d) {
buffer[len] = 0;
return len;
}
else {
lpoke(screen_line_address + len, c);
// Remove blink attribute from this char
lpoke(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS,
lpeek(len + screen_line_address + COLOUR_RAM_ADDRESS - SCREEN_ADDRESS) & 0xf);
buffer[len++] = c;
}
// *(unsigned char *)0x8000 = c;
// Clear key from hardware keyboard scanner
*(unsigned char *)0xd610 = 1;
}
}
return len;
}
#else
char read_line(char *buffer, unsigned char maxlen)
{
fgets(buffer, maxlen, stdin);
return strlen(buffer);
}
#endif