-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
575 lines (469 loc) · 15.4 KB
/
Copy pathmain.c
File metadata and controls
575 lines (469 loc) · 15.4 KB
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
/*
* main.c
*
* Created on: 13 de out de 2022
* Author: milton
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <curses.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <getopt.h>
#include "z80.h"
#include "romprog.h"
#include "ios.h"
#include "main.h"
#include "lcd.h"
#include "leds.h"
#include "vga.h"
////////////////////////////////////////////////////////////////////////////////
#define DEFAULT_MMAP 1
////////////////////////////////////////////////////////////////////////////////
#define RUN 1
#define DEBUGPROMPT 0
#define ROMSZ_MODE0 16384
#define RAMBASE_MODE0 16384
#define RAMSZ_MODE0 48*1024
#define ROMSZ_MODE1 8192
#define RAMBASE_MODE1 8192
#define RAMSZ_MODE1 56*1024
////////////////////////////////////////////////////////////////////////////////
#define MAIN_WIDTH 1280
#define MAIN_HEIGHT 960
#define LCD_X_OFFSET 5
#define LEDS_X_OFFSET 380
////////////////////////////////////////////////////////////////////////////////
void editprompt(char *buf, int size){
buf[0] = buf[1] = 0;
addstr("\n:");
refresh();
int pbuf = 0;
char cbuf[32];
for (;;){
cbuf[0] = getch();
if ((cbuf[0] == 8)||
(cbuf[0] == 127)){
if (pbuf) --pbuf;
else continue;
}
else
if (cbuf[0] == 10){
addstr("\n");
break;
}
else
if (pbuf < size)
buf[pbuf++] = cbuf[0];
if (cbuf[0] == 127){
addch(8);addch(' ');addch(8);
}
else
addch(cbuf[0]);
refresh();
}
buf[pbuf] = 0;
}
////////////////////////////////////////////////////////////////////////////////
void *z80runner(main_data_t *maindata){
#if DEBUGPROMPT
char buf[255];
#define NBP 4
uint16_t bp[1+NBP] = {0};
#endif
int num_steps = 0;
for (;/*!maindata->z.halted*/;){
if (!maindata->sdl->wminimized_main){
if (maindata->sdl->repaint_window_main){
vga_refresh(maindata->vga, 1);
maindata->sdl->repaint_window_main = 0;
}
else{
vga_refresh(maindata->vga, 0);
}
}
panel_check_refresh(maindata->panel);
sched_yield();
#if DEBUG_PROMPT
if (!maindata->z.running){
z80_dump_regs(&maindata->z);
//sprintf(buf,"Step:%d\n",num_steps);
//addstr(buf);
}
else{
for (int i = 0; i < 1+NBP; i++){
if ((maindata->z.pc == bp[i])&&bp[i]){
if (i == NBP)
bp[NBP] = 0;
maindata->z.running = 0;
maindata->z.iff1 = maindata->z.iff2 = 0;
z80_print(&maindata->z);
z80_dump_regs(&maindata->z);
//sprintf(buf,"Step:%d\n",num_steps);
//addstr(buf);
break;
}
}
}
#endif
z80_step(&maindata->z, maindata->ios);
num_steps++;
//printf("NextPC:%04x AfterPC:%04x\n",z.pc,z.afterPC);
if (maindata->z.running)
continue;
#if DEBUGPROMPT
prompt:
editprompt(buf,32);
if (!strncmp(buf,"rst",3)){
z80_reset(&maindata->z);
z80_print(&maindata->z);
num_steps = 0;
continue;
}
switch (buf[0]){
case 'q':
sprintf(buf,"\n==== NUM STEPS:%d ====\n",num_steps);
addstr(buf);
return NULL;
case 'h':
addstr(" # HELP #\n");
addstr(" ENTER ... Step Into\n");
addstr(" s ... Step Over RAM\n");
addstr(" d ... Dump RAM\n");
addstr(" bl ... List BKPTs\n");
addstr(" bcN (0-3) ... Clear BKPT N\n");
addstr(" bsN (0-3) ... Set BKPT N\n");
addstr(" g ... GO until BKPT or HALT\n");
addstr(" rst ... Reset CPU\n");
addstr(" q ... Quit\n");
goto prompt;
case 'd':
if (!isxdigit(buf[1]))
z80_dump_mem(&maindata->z, RAMBASE,512);
else{
int val;
sscanf(buf+1,"%04x",&val);
z80_dump_mem(&maindata->z, val,512);
}
goto prompt;
case 'b':
switch(buf[1]){
case 'l':
listbp:
addstr("\nBreakpoints\n");
for (int i = 0; i < NBP; i++){
sprintf(buf,"%d - %04x\n",i,bp[i]);
addstr(buf);
}
break;
case 'c':
if ((buf[2] >= '0') && (buf[2] <= '3')){
bp[buf[2]-'0'] = 0;
goto listbp;
}
break;
case 's':
if ((buf[2] >= '0') && (buf[2] <= '3')){
addstr("Enter BP val hhhh:");
char buf2[32];
editprompt(buf2,10);
int val;
sscanf(buf2,"%04x",&val);
bp[buf[2]-'0'] = val;
goto listbp;
}
break;
}
goto prompt;
case 'g':
z80_noprint(&maindata->z);
maindata->z.running = 1;
continue;
case 's':
if (maindata->z.afterPC){
bp[NBP] = maindata->z.afterPC;
z80_noprint(&maindata->z);
maindata->z.running = 1;
continue;
}
break;
}
#else
break;
#endif //#if DEBUGPROMPT
}
#if DEBUGPROMPT
sprintf(buf,"\n==== NUM STEPS:%d ====\n",num_steps);
addstr(buf);
#endif
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
typedef enum {
COD_VERSION = 1,
COD_RAMLOAD_IHX = 2,
COD_ROM1LOAD_IHX = 3,
COD_ROM2LOAD_IHX = 4,
COD_RAMLOAD_BIN = 5,
COD_MMAP = 6,
COD_PANEL = 7,
COD_DISKFNAME = 8,
COD_HELP = 9
} cod_option_t;
struct option longopts[] = {
{
"mmap", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_MMAP, //int val;
},
{
"loadramihx", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_RAMLOAD_IHX, //int val;
},
{
"loadram", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_RAMLOAD_BIN, //int val;
},
{
"rom1", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_ROM1LOAD_IHX, //int val;
},
{
"rom2", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_ROM2LOAD_IHX, //int val;
},
{
"disk", //const char *name;
required_argument, //int has_arg;
0, //int *flag;
COD_DISKFNAME, //int val;
},
{
"version", //const char *name;
0, //int has_arg;
0, //int *flag;
COD_VERSION, //int val;
},
{
"v", //const char *name;
0, //int has_arg;
0, //int *flag;
COD_VERSION, //int val;
},
{
"h", //const char *name;
0, //int has_arg;
0, //int *flag;
COD_HELP, //int val;
},
{
"help", //const char *name;
0, //int has_arg;
0, //int *flag;
COD_HELP, //int val;
},
{
"panel", //const char *name;
0, //int has_arg;
0, //int *flag;
COD_PANEL, //int val;
},
{
0, //const char *name;
0, //int has_arg;
0, //int *flag;
0, //int val;
}
};
////////////////////////////////////////////////////////////////////////////////
void print_version(){
printf("\nKraftSim v1.0.3\n(c)2026-04-28 ARMCoderBR\n\n");
}
////////////////////////////////////////////////////////////////////////////////
void print_help(){
print_version();
printf("Use:\n");
printf(" kraftsim -rom1 <imgfile.ihx> [-rom2 <imgfile.ihx>] [-loadram <imgfile.bin>] [-disk <imgdsk.vol>] [-mmap n] [-panel]\n");
printf(" At least one 'rom1' image must be loaded and must start at 0x0000.\n");
printf(" Images cannot be loaded to 'rom2' when using 'mmap 1'.\n");
printf(" Images loaded to RAM only make sense if the program in 'rom1' makes any use of it.\n");
#if DEFAULT_MMAP
printf(" '-mmap' defines the memory map 0 or 1 (default 1). Some ROMs may require 'mmap 0'.\n");
#else
printf(" '-mmap' defines the memory map 0 or 1 (default 0). Some ROMs may require 'mmap 1'.\n");
#endif
printf(" '-panel' shows the LCD/LEDs/Buttons panel on start.\n");
printf(" (You can also click on the main window to show the panel if hidden).\n");
}
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char *argv[]){
int res;
int longindex;
char filename[256] = {0};
char disk_filename[256] = {0};
#if DEFAULT_MMAP
int romSZ = ROMSZ_MODE1;
int ramBASE = RAMBASE_MODE1;
int ramSZ = RAMSZ_MODE1;
int appBase = 0x2000;
int mmap = 1;
#else
int romSZ = ROMSZ_MODE0;
int ramBASE = RAMBASE_MODE0;
int ramSZ = RAMSZ_MODE0;
int appBase = 0x4200;
int mmap = 0;
#endif
int loaded_rom = 0;
int show_panel = 0;
for (;;) {
res = getopt_long_only(argc, argv, "", longopts, &longindex);
if (res == -1) break;
if (res == 63)
return -1;
int val = longopts[longindex].val;
switch (val) {
case COD_HELP:
print_help();
return 0;
case COD_VERSION:
print_version();
return 0;
case COD_MMAP:
//if (optarg)
#if DEFAULT_MMAP
if (optarg[0] == '0'){
romSZ = ROMSZ_MODE0;
ramBASE = RAMBASE_MODE0;
ramSZ = RAMSZ_MODE0;
appBase = 0x4200;
mmap = 0;
}
#else
if (optarg[0] == '1'){
romSZ = ROMSZ_MODE1;
ramBASE = RAMBASE_MODE1;
ramSZ = RAMSZ_MODE1;
appBase = 0x2000;
mmap = 1;
}
#endif
break;
}
}
main_data_t maindata;
////////////////////////////////////////////////////////////////////////////
maindata.rom = malloc(romSZ);
maindata.ram = malloc(ramSZ);
memset(maindata.rom,0xff,romSZ);
memset(maindata.ram,0x00,ramSZ);
optind = 1; // Reinicia busca das opções
for (;;) {
res = getopt_long_only(argc, argv, "", longopts, &longindex);
if ((res == -1) || (res == 63))
break;
int val = longopts[longindex].val;
switch (val) {
case COD_RAMLOAD_IHX:
//if (optarg){
strncpy(filename,optarg,sizeof(filename));
res = memload_ihx(maindata.ram, ramBASE, ramSZ, filename, 0x0000);
if (res < 0){
return -1;
}
//}
break;
case COD_RAMLOAD_BIN:
//if (optarg){
strncpy(filename,optarg,sizeof(filename));
res = memload_bin(maindata.ram, ramBASE, ramSZ, filename, appBase);
if (res < 0){
return -1;
}
//}
break;
case COD_ROM1LOAD_IHX:
//if (optarg){
loaded_rom = 1;
strncpy(filename,optarg,sizeof(filename));
res = memload_ihx(maindata.rom, 0, romSZ, filename, 0x0000);
if (res < 0){
return -1;
}
//}
break;
case COD_ROM2LOAD_IHX:
if (mmap){
printf("ROM2 not allowed in mmap 1. Select mmap 0.\n");
return 0;
}
//if (optarg){
strncpy(filename,optarg,sizeof(filename));
res = memload_ihx(maindata.rom, 0, romSZ, filename, 0x2000);
if (res < 0){
return -1;
}
//}
break;
case COD_DISKFNAME:
//if (optarg){
strncpy(disk_filename,optarg,sizeof(disk_filename));
//}
break;
case COD_PANEL:
show_panel = 1;
break;
}
}
if (!loaded_rom){
printf("You need to load at least one ROM1 image.\n");
return 0;
}
maindata.width = MAIN_WIDTH;
maindata.height = MAIN_HEIGHT;
maindata.sdl = sdl_init (maindata.width, maindata.height);
if (!maindata.sdl){
printf("Error starting SDL!\n");
return -1;
}
maindata.panel = panel_init(show_panel);
maindata.vga = vga_init(maindata.sdl->renderer_main);
maindata.ios = ios_init(&maindata,disk_filename);
initscr();
////usleep(1000000);
idlok(stdscr,TRUE);
scrollok(stdscr,TRUE);
noecho();
z80_initialize(&maindata.z, maindata.rom, romSZ, maindata.ram, ramBASE, ramSZ, new_out_callback, new_in_callback, new_hw_run, new_irq_sample);
z80_reset(&maindata.z);
#if RUN
z80_noprint(&maindata.z);
maindata.z.running = 1;
#else
z80_print(&maindata.z);
maindata->z.running = 0;
#endif
z80runner(&maindata);
//z80_dump_mem(&maindata.z, RAMBASE,512);
ios_close(maindata.ios);
free(maindata.rom);
free(maindata.ram);
panel_close(maindata.panel);
vga_close(maindata.vga);
sdl_close(maindata.sdl);
endwin();
return 0;
}