-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack64.ino
349 lines (281 loc) · 7.51 KB
/
stack64.ino
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
// C64 Emulator
// http://forum.arduino.cc/index.php?topic=193216.msg1793065#msg1793065
#include <M5Stack.h>
uint8_t curkey = 0;
bool onscreenmenu = false;
bool redraw = false;
bool mempull = false;
uint8_t slotid;
#define KEYBOARD_I2C_ADDR 0X08
#define KEYBOARD_INT 5
#define RAM_SIZE 30816 //SOMEWHAT LESS THAN 32kB
uint8_t RAM[RAM_SIZE];
uint8_t VRAM[1000];
uint16_t getpc();
uint8_t getop();
void exec6502(int32_t tickcount);
void reset6502();
void serout(uint8_t val) {
Serial.write(val);
M5.Lcd.print(val);
}
uint8_t getkey() {
return (curkey);
}
void clearkey() {
curkey = 0;
}
/*
void printhex(uint16_t val) {
Serial.print(val, HEX);
Serial.println();
M5.Lcd.print(val);
}*/
void VTposition(uint8_t row, uint8_t col) {
//Serial.write(27);
//Serial.write('[');
//Serial.print(row + 1);
//Serial.write(';');
//Serial.print(col + 1);
//Serial.write('H');
M5.Lcd.setCursor((col * 6) + 40, (row * 9) + 7);
}
void drawscreen() {
M5.Lcd.setTextColor(WHITE);
uint16_t v_address = 0;
uint16_t last_v_address = 0;
uint8_t col_c = 0;
uint8_t petscii;
//M5.Lcd.fillScreen(BLUE); //clear screen
//do I need a row redraw?
//if (VRAM[1] != RAM[1 + 1024]) M5.Lcd.fillScreen(BLUE);
// if (VRAM[1000-49] != RAM[(1000-49) + 1024]) redraw = true;
// if (VRAM[0] != RAM[0 + 1024]) redraw = true;
// if (VRAM[1] != RAM[1 + 1024]) redraw = true;
// if (VRAM[2] != RAM[2 + 1024]) redraw = true;
// if (VRAM[3] != RAM[3 + 1024]) redraw = true;
// if (VRAM[4] != RAM[4 + 1024]) redraw = true;
// if (VRAM[5] != RAM[5 + 1024]) redraw = true;
if (redraw) {
M5.Lcd.fillScreen(BLUE);
redraw = false;
}
for (uint8_t row = 0; row < 25; row++) {
for (uint8_t col = 0; col < 40; col++) {
petscii = RAM[v_address + 1024];
//if (VRAM[v_address] != petscii) {
VRAM[v_address] = petscii;
// redraw = true;
//}
VTposition(row, col);
if (petscii < 32) petscii = petscii + 64;
M5.Lcd.print((char)(petscii));
v_address++;
}
}
}
void readbuttons() {
if (M5.BtnA.wasReleased()) {
/*
onscreenmenu = !onscreenmenu;
if (onscreenmenu) {
drawonscreenmenu();
}*/
uint8_t sid;
sid = getSlotNumber(SPIFFS);
Serial.printf("pre slot: %u\r\n", sid);
sid = sid +1;
if (sid > 9) sid = 0;
setSlotNumber(SPIFFS, sid);
Serial.printf("post slot: %u\r\n", sid);
M5.Lcd.setCursor(60,200);
M5.Lcd.setTextSize(4);
M5.Lcd.printf("%u", sid);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(140,220);
M5.Lcd.printf("LOAD");
M5.Lcd.setCursor(240,220);
M5.Lcd.printf("SAVE", sid);
M5.Lcd.setTextSize(1);
slotid = sid;
//drawonscreenmenu();
delay(500);
redraw = true;
}
else if (M5.BtnB.wasReleased()) {
M5.Lcd.fillScreen(BLUE);
M5.Lcd.setCursor(0,0);
uint8_t sid;
sid = getSlotNumber(SPIFFS);
Serial.printf("Slot: %u\r\n", sid);
M5.Lcd.printf("Slot: %u\r\n", sid);
char str[100];
listDir(SPIFFS, "/", 0);
sprintf(str,"/memory.%u",sid);
Serial.println(str);
//pullMemory(SPIFFS, "/"+(char)sid"/memory.c64");
pullMemory(SPIFFS, str);
sprintf(str,"/cpu.%u",sid);
Serial.println(str);
//pullCPU(SPIFFS, "/"+(char)sid"/cpu.c64");
pullCPU(SPIFFS, str);
delay(1000);
redraw = true;
}
else if (M5.BtnC.wasReleased()) {
M5.Lcd.fillScreen(BLUE);
M5.Lcd.setCursor(0,0);
uint8_t sid;
sid = getSlotNumber(SPIFFS);
Serial.printf("Slot: %u\r\n", sid);
M5.Lcd.printf("Slot: %u\r\n", sid);
char str[100];
sprintf(str,"/memory.%u",sid);
Serial.println(str);
//pushMemory(SPIFFS, "/memory.c64");
pushMemory(SPIFFS, str);
sprintf(str,"/cpu.%u",sid);
Serial.println(str);
//pushCPU(SPIFFS, "/cpu.c64");
pushCPU(SPIFFS, str);
listDir(SPIFFS, "/", 0);
delay(1000);
redraw = true;
mempull = true;
}
else if (M5.BtnA.wasReleasefor(700)) {
M5.powerOFF();
}
}
uint8_t coverttouppercase(uint8_t dirtykeyval) {
uint8_t cleankeyval;
if (dirtykeyval >= 0x61 && dirtykeyval < 0x7A) {
cleankeyval = dirtykeyval - 32;
}
else {
cleankeyval = dirtykeyval;
}
return cleankeyval;
}
void readkeyboard() {
/*if (Serial.available()) {
curkey = Serial.read() & 0x7F;
RAM[198] = 1;
RAM[631] = curkey;
}*/
if (digitalRead(KEYBOARD_INT) == LOW) {
Wire.requestFrom(KEYBOARD_I2C_ADDR, 1); // request 1 byte from keyboard
while (Wire.available()) {
uint8_t key_val = Wire.read(); // receive a byte as character
if (key_val != 0) {
curkey = coverttouppercase(key_val);// key_val & 0x7F;
RAM[198] = 1;
RAM[631] = curkey;
if (curkey == 0x0D) redraw = true;
/*if (key_val >= 0x20 && key_val < 0x7F) { // ASCII String
Serial.print((char)key_val);
//M5.Lcd.print((char)key_val);
} else {
Serial.printf("0x%02X ", key_val);
//M5.Lcd.printf("0x%02X ", key_val);
}*/
}
}
}
}
void setup () {
// initialize the M5Stack object
M5.begin(); // M5dstack
Wire.begin(); // GPIO
M5.Lcd.setTextSize(1); // LCD text size 1
pinMode(KEYBOARD_INT, INPUT_PULLUP); // Keyboaerd
Serial.println("");
M5.Lcd.println("");
//WiFi.mode(WIFI_OFF);
Serial.begin (115200);
Serial.setDebugOutput(true);
//delay(10000);//needed to let user open terminal/monitor
Serial.println("");
M5.Lcd.println("");
Serial.println("Persistence data..");
M5.Lcd.println("Persistence data..");
persistenceinit();
Serial.println ("Prep VRAM..");
M5.Lcd.println("Prep VRAM..");
for (int i = 0; i < 1000; i++) {
VRAM[i] = RAM[i + 1024];
}
Serial.println ("Init CPU..");
M5.Lcd.println ("Init CPU..");
reset6502();
Serial.println ("Starting CPU..");
M5.Lcd.println("Starting CPU..");
//delay(2000);
RAM[198] = 0;
exec6502(200000);
slotid = getSlotNumber(SPIFFS);
Serial.printf("pre slot: %u\r\n", slotid);
M5.Lcd.fillScreen(BLUE); //clear screen
}
int counter = 1;
int effc = 1;
void drawslot(){
M5.Lcd.setCursor(305,220);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("%u", slotid);
M5.Lcd.setTextSize(1);
}
bool needredraw() {
uint16_t v_address = 0;
//redraw = false;
for (uint8_t row = 0; row < 25; row++) {
for (uint8_t col = 0; col < 40; col++) {
if (VRAM[v_address] != RAM[v_address + 1024]) {
redraw = true;
}
v_address++;
}
}
}
void loop(){
M5.update();
readkeyboard();
exec6502(40*25*100); //(40*25*200);
needredraw();
drawscreen();
readbuttons();
drawslot();
}
/*
void loop () {
uint16_t v_address = 0;
uint16_t last_v_address = 0;
int eff = 0;
uint8_t col_c = 0;
Serial.print("\x1b[H");
//M5.Lcd.fillScreen(BLUE); //clear screen
M5.Lcd.setTextColor(WHITE);
for (uint8_t row = 0; row < 25; row++) {
for (uint8_t col = 0; col < 40; col++) {
readkeyboard();
exec6502(200); //faster stack allows more ins per loop
uint8_t petscii = RAM[v_address + 1024];
if (VRAM[v_address] != petscii) {
VRAM[v_address] = petscii;
if (((v_address - last_v_address) > 1) || (col_c >= 40)) {
VTposition(row, col);
col_c = col;
}
if (petscii < 32) petscii = petscii + 64;
//Serial.write(petscii);
M5.Lcd.print((char)(petscii));
last_v_address = v_address;
}
col_c++;
v_address++;
}
}
readbutton();
M5.update();
}
*/