-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv68doscall.c
776 lines (749 loc) · 22.1 KB
/
v68doscall.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "v68.h"
#include "v68io.h"
#include "musashi/m68k.h"
#include "musashi/m68kcpu.h"
#include "sjis.h"
#define DOS_CALL_EXIT 0x00
#define DOS_CALL_GETCHAR 0x01
#define DOS_CALL_PUTCHAR 0x02
#define DOS_CALL_COMINP 0x03
#define DOS_CALL_COMOUT 0x04
#define DOS_CALL_PRNOUT 0x05
#define DOS_CALL_INPOUT 0x06
#define DOS_CALL_INKEY 0x07
#define DOS_CALL_GETC 0x08
#define DOS_CALL_PRINT 0x09
#define DOS_CALL_GETS 0x0a
#define DOS_CALL_KEYSNS 0x0b
#define DOS_CALL_KFLUSH 0x0c
#define DOS_CALL_FFLUSH 0x0d
#define DOS_CALL_CHGDRV 0x0e
#define DOS_CALL_DRVCTRL 0x0f
#define DOS_CALL_CONSNS 0x10
#define DOS_CALL_PRNSNS 0x11
#define DOS_CALL_CINSNS 0x12
#define DOS_CALL_COUTSNS 0x13
#define DOS_CALL_FATCHK 0x17
#define DOS_CALL_HENDSP 0x18
#define DOS_CALL_CURDRV 0x19
#define DOS_CALL_GETSS 0x1a
#define DOS_CALL_FGETC 0x1b
#define DOS_CALL_FGETS 0x1c
#define DOS_CALL_FPUTC 0x1d
#define DOS_CALL_FPUTS 0x1e
#define DOS_CALL_ALLCLOSE 0x1f
#define DOS_CALL_SUPER 0x20
#define DOS_CALL_FNCKEY 0x21
#define DOS_CALL_KNJCTRL 0x22
#define DOS_CALL_CONCTRL 0x23
#define DOS_CALL_KEYCTRL 0x24
#define DOS_CALL_INTVCS 0x25
#define DOS_CALL_PSPSET 0x26
#define DOS_CALL_GETTIM2 0x27
#define DOS_CALL_SETTIM2 0x28
#define DOS_CALL_NAMESTS 0x29
#define DOS_CALL_GETDATE 0x2a
#define DOS_CALL_SETDATE 0x2b
#define DOS_CALL_GETTIME 0x2c
#define DOS_CALL_SETTIME 0x2d
#define DOS_CALL_VERIFY 0x2e
#define DOS_CALL_DUP0 0x2f
#define DOS_CALL_VERNUM 0x30
#define DOS_CALL_KEEPPR 0x31
#define DOS_CALL_GETDPB 0x32
#define DOS_CALL_BREAKCK 0x33
#define DOS_CALL_DRVXCHG 0x34
#define DOS_CALL_INTVCG 0x35
#define DOS_CALL_DSKFRE 0x36
#define DOS_CALL_NAMECK 0x37
#define DOS_CALL_MKDIR 0x39
#define DOS_CALL_RMDIR 0x3a
#define DOS_CALL_CHDIR 0x3b
#define DOS_CALL_CREATE 0x3c
#define DOS_CALL_OPEN 0x3d
#define DOS_CALL_CLOSE 0x3e
#define DOS_CALL_READ 0x3f
#define DOS_CALL_WRITE 0x40
#define DOS_CALL_DELETE 0x41
#define DOS_CALL_SEEK 0x42
#define DOS_CALL_CHMOD 0x43
#define DOS_CALL_IOCTRL 0x44
#define DOS_CALL_DUP 0x45
#define DOS_CALL_DUP2 0x46
#define DOS_CALL_CURDIR 0x47
#define DOS_CALL_MALLOC 0x48
#define DOS_CALL_MFREE 0x49
#define DOS_CALL_SETBLOCK 0x4a
#define DOS_CALL_EXEC 0x4b
#define DOS_CALL_EXIT2 0x4c
#define DOS_CALL_WAIT 0x4d
#define DOS_CALL_FILES 0x4e
#define DOS_CALL_NFILES 0x4f
#define DOS_CALL_SETPDB 0x80
#define DOS_CALL_GETPDB 0x81
#define DOS_CALL_SETENV 0x82
#define DOS_CALL_GETENV 0x83
#define DOS_CALL_VERIFYG 0x84
#define DOS_CALL_COMMON 0x85
#define DOS_CALL_RENAME 0x86
#define DOS_CALL_FILEDATE 0x87
#define DOS_CALL_MALLOC2 0x88
#define DOS_CALL_MAKETMP 0x8a
#define DOS_CALL_NEWFILE 0x8b
#define DOS_CALL_LOCK 0x8c
#define DOS_CALL_ASSIGN 0x8f
#define DOS_CALL_FFLUSH_SET 0xaa
#define DOS_CALL_OS_PATCH 0xab
#define DOS_CALL_GETFCB 0xac
#define DOS_CALL_S_MALLOC 0xad
#define DOS_CALL_S_MFREE 0xae
#define DOS_CALL_S_PROCESS 0xaf
#define DOS_CALL_EXITVC 0xf0
#define DOS_CALL_CTRLVC 0xf1
#define DOS_CALL_ERRJVC 0xf2
#define DOS_CALL_DISKRED 0xf3
#define DOS_CALL_DISKWRT 0xf4
#define DOS_CALL_INDOSFLG 0xf5
#define DOS_CALL_SUPER_JSR 0xf6
#define DOS_CALL_BUS_ERR 0xf7
#define DOS_CALL_OPEN_PR 0xf8
#define DOS_CALL_KILL_PR 0xf9
#define DOS_CALL_GET_PR 0xfa
#define DOS_CALL_SUSPEND_PR 0xfb
#define DOS_CALL_SLEEP_PR 0xfc
#define DOS_CALL_SEND_PR 0xfd
#define DOS_CALL_TIME_PR 0xfe
#define DOS_CALL_CHANGE_PR 0xff
static const char *dos_call_names[256] = {
/* 0x00 */ "_EXIT",
/* 0x01 */ "_GETCHAR",
/* 0x02 */ "_PUTCHAR",
/* 0x03 */ "_COMINP",
/* 0x04 */ "_COMOUT",
/* 0x05 */ "_PRNOUT",
/* 0x06 */ "_INPOUT",
/* 0x07 */ "_INKEY",
/* 0x08 */ "_GETC",
/* 0x09 */ "_PRINT",
/* 0x0a */ "_GETS",
/* 0x0b */ "_KEYSNS",
/* 0x0c */ "_KFLUSH",
/* 0x0d */ "_FFLUSH",
/* 0x0e */ "_CHGDRV",
/* 0x0f */ "_DRVCTRL",
/* 0x10 */ "_CONSNS",
/* 0x11 */ "_PRNSNS",
/* 0x12 */ "_CINSNS",
/* 0x13 */ "_COUTSNS",
/* 0x14 */ "?",
/* 0x15 */ "?",
/* 0x16 */ "?",
/* 0x17 */ "_FATCHK",
/* 0x18 */ "_HENDSP",
/* 0x19 */ "_CURDRV",
/* 0x1a */ "_GETSS",
/* 0x1b */ "_FGETC",
/* 0x1c */ "_FGETS",
/* 0x1d */ "_FPUTC",
/* 0x1e */ "_FPUTS",
/* 0x1f */ "_ALLCLOSE",
/* 0x20 */ "_SUPER",
/* 0x21 */ "_FNCKEY",
/* 0x22 */ "_KNJCTRL",
/* 0x23 */ "_CONCTRL",
/* 0x24 */ "_KEYCTRL",
/* 0x25 */ "_INTVCS",
/* 0x26 */ "_PSPSET",
/* 0x27 */ "_GETTIM2",
/* 0x28 */ "_SETTIM2",
/* 0x29 */ "_NAMESTS",
/* 0x2a */ "_GETDATE",
/* 0x2b */ "_SETDATE",
/* 0x2c */ "_GETTIME",
/* 0x2d */ "_SETTIME",
/* 0x2e */ "_VERIFY",
/* 0x2f */ "_DUP0",
/* 0x30 */ "_VERNUM",
/* 0x31 */ "_KEEPPR",
/* 0x32 */ "_GETDPB",
/* 0x33 */ "_BREAKCK",
/* 0x34 */ "_DRVXCHG",
/* 0x35 */ "_INTVCG",
/* 0x36 */ "_DSKFRE",
/* 0x37 */ "_NAMECK",
/* 0x38 */ "?",
/* 0x39 */ "_MKDIR",
/* 0x3a */ "_RMDIR",
/* 0x3b */ "_CHDIR",
/* 0x3c */ "_CREATE",
/* 0x3d */ "_OPEN",
/* 0x3e */ "_CLOSE",
/* 0x3f */ "_READ",
/* 0x40 */ "_WRITE",
/* 0x41 */ "_DELETE",
/* 0x42 */ "_SEEK",
/* 0x43 */ "_CHMOD",
/* 0x44 */ "_IOCTRL",
/* 0x45 */ "_DUP",
/* 0x46 */ "_DUP2",
/* 0x47 */ "_CURDIR",
/* 0x48 */ "_MALLOC",
/* 0x49 */ "_MFREE",
/* 0x4a */ "_SETBLOCK",
/* 0x4b */ "_EXEC",
/* 0x4c */ "_EXIT2",
/* 0x4d */ "_WAIT",
/* 0x4e */ "_FILES",
/* 0x4f */ "_NFILES",
/* 0x50 */ "?",
/* 0x51 */ "?",
/* 0x52 */ "?",
/* 0x53 */ "?",
/* 0x54 */ "?",
/* 0x55 */ "?",
/* 0x56 */ "?",
/* 0x57 */ "?",
/* 0x58 */ "?",
/* 0x59 */ "?",
/* 0x5a */ "?",
/* 0x5b */ "?",
/* 0x5c */ "?",
/* 0x5d */ "?",
/* 0x5e */ "?",
/* 0x5f */ "?",
/* 0x60 */ "?",
/* 0x61 */ "?",
/* 0x62 */ "?",
/* 0x63 */ "?",
/* 0x64 */ "?",
/* 0x65 */ "?",
/* 0x66 */ "?",
/* 0x67 */ "?",
/* 0x68 */ "?",
/* 0x69 */ "?",
/* 0x6a */ "?",
/* 0x6b */ "?",
/* 0x6c */ "?",
/* 0x6d */ "?",
/* 0x6e */ "?",
/* 0x6f */ "?",
/* 0x70 */ "?",
/* 0x71 */ "?",
/* 0x72 */ "?",
/* 0x73 */ "?",
/* 0x74 */ "?",
/* 0x75 */ "?",
/* 0x76 */ "?",
/* 0x77 */ "?",
/* 0x78 */ "?",
/* 0x79 */ "?",
/* 0x7a */ "?",
/* 0x7b */ "?",
/* 0x7c */ "?",
/* 0x7d */ "?",
/* 0x7e */ "?",
/* 0x7f */ "?",
/* 0x80 */ "_SETPDB",
/* 0x81 */ "_GETPDB",
/* 0x82 */ "_SETENV",
/* 0x83 */ "_GETENV",
/* 0x84 */ "_VERIFYG",
/* 0x85 */ "_COMMON",
/* 0x86 */ "_RENAME",
/* 0x87 */ "_FILEDATE",
/* 0x88 */ "_MALLOC2",
/* 0x89 */ "?",
/* 0x8a */ "_MAKETMP",
/* 0x8b */ "_NEWFILE",
/* 0x8c */ "_LOCK",
/* 0x8d */ "?",
/* 0x8e */ "?",
/* 0x8f */ "_ASSIGN",
/* 0x90 */ "?",
/* 0x91 */ "?",
/* 0x92 */ "?",
/* 0x93 */ "?",
/* 0x94 */ "?",
/* 0x95 */ "?",
/* 0x96 */ "?",
/* 0x97 */ "?",
/* 0x98 */ "?",
/* 0x99 */ "?",
/* 0x9a */ "?",
/* 0x9b */ "?",
/* 0x9c */ "?",
/* 0x9d */ "?",
/* 0x9e */ "?",
/* 0x9f */ "?",
/* 0xa0 */ "?",
/* 0xa1 */ "?",
/* 0xa2 */ "?",
/* 0xa3 */ "?",
/* 0xa4 */ "?",
/* 0xa5 */ "?",
/* 0xa6 */ "?",
/* 0xa7 */ "?",
/* 0xa8 */ "?",
/* 0xa9 */ "?",
/* 0xaa */ "_FFLUSH_SET",
/* 0xab */ "_OS_PATCH",
/* 0xac */ "_GETFCB",
/* 0xad */ "_S_MALLOC",
/* 0xae */ "_S_MFREE",
/* 0xaf */ "_S_PROCESS",
/* 0xb0 */ "?",
/* 0xb1 */ "?",
/* 0xb2 */ "?",
/* 0xb3 */ "?",
/* 0xb4 */ "?",
/* 0xb5 */ "?",
/* 0xb6 */ "?",
/* 0xb7 */ "?",
/* 0xb8 */ "?",
/* 0xb9 */ "?",
/* 0xba */ "?",
/* 0xbb */ "?",
/* 0xbc */ "?",
/* 0xbd */ "?",
/* 0xbe */ "?",
/* 0xbf */ "?",
/* 0xc0 */ "?",
/* 0xc1 */ "?",
/* 0xc2 */ "?",
/* 0xc3 */ "?",
/* 0xc4 */ "?",
/* 0xc5 */ "?",
/* 0xc6 */ "?",
/* 0xc7 */ "?",
/* 0xc8 */ "?",
/* 0xc9 */ "?",
/* 0xca */ "?",
/* 0xcb */ "?",
/* 0xcc */ "?",
/* 0xcd */ "?",
/* 0xce */ "?",
/* 0xcf */ "?",
/* 0xd0 */ "?",
/* 0xd1 */ "?",
/* 0xd2 */ "?",
/* 0xd3 */ "?",
/* 0xd4 */ "?",
/* 0xd5 */ "?",
/* 0xd6 */ "?",
/* 0xd7 */ "?",
/* 0xd8 */ "?",
/* 0xd9 */ "?",
/* 0xda */ "?",
/* 0xdb */ "?",
/* 0xdc */ "?",
/* 0xdd */ "?",
/* 0xde */ "?",
/* 0xdf */ "?",
/* 0xe0 */ "?",
/* 0xe1 */ "?",
/* 0xe2 */ "?",
/* 0xe3 */ "?",
/* 0xe4 */ "?",
/* 0xe5 */ "?",
/* 0xe6 */ "?",
/* 0xe7 */ "?",
/* 0xe8 */ "?",
/* 0xe9 */ "?",
/* 0xea */ "?",
/* 0xeb */ "?",
/* 0xec */ "?",
/* 0xed */ "?",
/* 0xee */ "?",
/* 0xef */ "?",
/* 0xf0 */ "_EXITVC",
/* 0xf1 */ "_CTRLVC",
/* 0xf2 */ "_ERRJVC",
/* 0xf3 */ "_DISKRED",
/* 0xf4 */ "_DISKWRT",
/* 0xf5 */ "_INDOSFLG",
/* 0xf6 */ "_SUPER_JSR",
/* 0xf7 */ "_BUS_ERR",
/* 0xf8 */ "_OPEN_PR",
/* 0xf9 */ "_KILL_PR",
/* 0xfa */ "_GET_PR",
/* 0xfb */ "_SUSPEND_PR",
/* 0xfc */ "_SLEEP_PR",
/* 0xfd */ "_SEND_PR",
/* 0xfe */ "_TIME_PR",
/* 0xff */ "_CHANGE_PR",
};
int v68_dos_call(uint16_t instr) {
uint8_t call = instr & 0xff;
if(v68.log_calls)
printf("DOS %02x %s\n", call, dos_call_names[call]);
switch(call) {
case DOS_CALL_EXIT:
v68.running = 0;
v68_mem_free(v68.cur_prog_addr, m68k_read_memory_32(v68.cur_prog_addr - 0x0c));
v68_queue_next_command();
break;
case DOS_CALL_EXIT2:
v68.running = 0;
v68_mem_free(v68.cur_prog_addr, m68k_read_memory_32(v68.cur_prog_addr - 0x0c));
v68_queue_next_command();
break;
case DOS_CALL_PRINT: {
uint32_t str_addr = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
logcall("str=%08x\n", str_addr);
if(str_addr < v68.ram_size) {
uint8_t *str_ptr = &v68.ram[str_addr];
size_t l = strnlen((char *)str_ptr, v68.ram_size - str_addr);
sjis_print_utf8(str_ptr, l);
fflush(stdout);
}
}
break;
case DOS_CALL_PUTCHAR: {
uint16_t chr = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint8_t chrbuf[2] = { chr >> 8, chr };
sjis_print_utf8(chrbuf[0] ? chrbuf : chrbuf+1, chrbuf[0] ? 2 : 1);
}
break;
case DOS_CALL_KEEPPR: {
uint32_t prglen = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint16_t code = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 4);
logcall("prglen=%d (%dkB) code=%04x\n", prglen, prglen/1024, code);
v68_mem_dump();
logcall("shrinking mem 0x%08x\n", v68.cur_prog_addr);
v68_mem_shrink(v68.cur_prog_addr, prglen);
v68_mem_dump();
v68_queue_next_command();
}
break;
case DOS_CALL_SUPER: {
uint32_t sp = m68k_get_reg(0, M68K_REG_A7);
uint32_t stack = m68k_read_memory_32(sp);
logcall("stack=%08x\n", stack);
FLAG_S = 1;
m68k_set_reg(M68K_REG_SR, m68k_get_reg(0, M68K_REG_SR) | 0x2000);
m68k_set_reg(M68K_REG_A7, sp);
}
break;
case DOS_CALL_IOCTRL: {
uint16_t md = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
switch(md) {
case 0: {
uint16_t fileno = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 2);
if(fileno == 0) m68k_set_reg(M68K_REG_D0, 0x80c1);
else if(fileno == 1 || fileno == 2) m68k_set_reg(M68K_REG_D0, 0x80c2);
else m68k_set_reg(M68K_REG_D0, 0);
}
break;
default:
logcall("IOCTRL %04x\n", md);
}
}
break;
case DOS_CALL_SETBLOCK: {
uint32_t memptr = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint32_t len = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 4);
logcall("memptr=0x%08x len=%d\n", memptr, len);
v68_mem_dump();
logcall("Shrinking mem\n");
int r = v68_mem_shrink(memptr, len);
v68_mem_dump();
m68k_set_reg(M68K_REG_D0, r);
}
break;
case DOS_CALL_MALLOC: {
uint32_t len = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint32_t remaining = v68_mem_remaining();
if(len > remaining) len = remaining;
uint32_t m = v68_mem_alloc(len, v68.cur_prog_addr);
logcall("len=0x%08x (%d) result=0x%08x\n", len, len, m);
m68k_set_reg(M68K_REG_D0, m);
v68_mem_dump();
}
break;
case DOS_CALL_FPUTS: {
uint32_t mesptr = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint16_t fileno = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 4);
uint8_t *str_ptr = &v68.ram[mesptr];
size_t l = strnlen((char *)str_ptr, v68.ram_size - mesptr);
if(fileno == 2) {
sjis_print_utf8(str_ptr, l);
} else {
logcall("FPUTS mesptr=%08x \"%s\" fileno=%d\n", mesptr, v68.ram + mesptr, fileno);
}
}
break;
case DOS_CALL_VERNUM: {
m68k_set_reg(M68K_REG_D0, 0x36380302);
}
break;
case DOS_CALL_GETDATE: {
#if __linux__ || __APPLE__
time_t t = time(NULL);
struct tm result;
localtime_r(&t, &result);
uint32_t d = ((result.tm_wday & 0x07) << 16) | (((result.tm_year - 80) & 0x7f) << 9) | ((result.tm_mon & 0x0f) << 5) | (result.tm_mday & 0x1f);
m68k_set_reg(M68K_REG_D0, d);
#endif
}
break;
case DOS_CALL_GETTIME: {
#if __linux__ || __APPLE__
time_t t = time(NULL);
struct tm result;
localtime_r(&t, &result);
uint16_t d = ((result.tm_hour & 0x1f) << 11) | ((result.tm_min & 0x3f) << 5) | ((result.tm_sec >> 1) & 0x1f);
m68k_set_reg(M68K_REG_D0, d);
#endif
}
break;
case DOS_CALL_GETTIM2: {
#if __linux__ || __APPLE__
time_t t = time(NULL);
struct tm result;
localtime_r(&t, &result);
uint16_t d = ((result.tm_hour & 0x1f) << 16) | ((result.tm_min & 0x3f) << 8) | (result.tm_sec & 0x3f);
m68k_set_reg(M68K_REG_D0, d);
#endif
}
break;
case DOS_CALL_CREATE: {
uint32_t file = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint16_t attr = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 4);
uint8_t *str_ptr = &v68.ram[file];
size_t l = strnlen((char *)str_ptr, v68.ram_size - file - 1);
str_ptr[l] = 0;
logcall("filename=%s attr=0x%04x\n", str_ptr, attr);
int fd = v68_io_create((char *)str_ptr, attr);
m68k_set_reg(M68K_REG_D0, fd);
/* Fill fd bitmap */
if(fd > 0) {
fd -= 4;
uint32_t fdmapaddr = v68.cur_prog_addr + 0x24 + (fd >> 3);
m68k_write_memory_8(fdmapaddr, m68k_read_memory_8(fdmapaddr) | (1 << (fd & 0x07)));
}
}
break;
case DOS_CALL_OPEN: {
uint32_t file = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint16_t mode = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 4);
uint8_t *str_ptr = &v68.ram[file];
size_t l = strnlen((char *)str_ptr, v68.ram_size - file - 1);
str_ptr[l] = 0;
int fd = v68_io_open((char *)str_ptr, mode);
logcall("filename=\"%s\" mode=0x%04x fd=%d\n", str_ptr, mode, fd);
m68k_set_reg(M68K_REG_D0, fd);
/* Fill fd bitmap */
if(fd > 0) {
fd -= 4;
uint32_t fdmapaddr = v68.cur_prog_addr + 0x24 + (fd >> 3);
m68k_write_memory_8(fdmapaddr, m68k_read_memory_8(fdmapaddr) | (1 << (fd & 0x07)));
}
}
break;
case DOS_CALL_CLOSE: {
uint16_t fd = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
int r = v68_io_close(fd);
m68k_set_reg(M68K_REG_D0, r);
logcall("fd=%d\n", fd);
/* Unset bit from fd bitmap */
if(!r) {
fd -= 4;
uint32_t fdmapaddr = v68.cur_prog_addr + 0x24 + (fd >> 3);
m68k_write_memory_8(fdmapaddr, m68k_read_memory_8(fdmapaddr) & ~(1 << (fd & 0x07)));
}
}
break;
case DOS_CALL_WRITE: {
uint16_t fd = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint32_t buffer = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 2);
uint32_t len = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 6);
int r = v68_io_write(fd, &v68.ram[buffer], len);
logcall("fd=%d buffer=0x%08x len=%d r=%d\n", fd, buffer, len, r);
m68k_set_reg(M68K_REG_D0, r);
}
break;
case DOS_CALL_READ: {
uint16_t fd = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint32_t buffer = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 2);
uint32_t len = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 6);
int pos_before = v68_io_tell(fd);
int r = v68_io_read(fd, &v68.ram[buffer], len);
logcall("fd=%d buffer=0x%08x len=%d r=%d pos_before=0x%08x pos=0x%08x\n", fd, buffer, len, r, pos_before, v68_io_tell(fd));
m68k_set_reg(M68K_REG_D0, r);
}
break;
case DOS_CALL_FGETC: {
uint16_t fd = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint8_t b = 0;
int r = v68_io_read(fd, &b, 1);
logcall("fd=%d r=%d\n", fd, r);
m68k_set_reg(M68K_REG_D0, b);
}
break;
case DOS_CALL_SEEK: {
uint16_t fd = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint32_t ofs = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 2);
uint32_t whence = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7) + 6);
int r = v68_io_seek(fd, ofs, whence);
logcall("fd=%d ofs=0x%08x whence=0x%04x r=0x%08x pos=0x%08x\n", fd, ofs, whence, r, v68_io_tell(fd));
m68k_set_reg(M68K_REG_D0, r);
}
break;
case DOS_CALL_INTVCS: {
uint16_t intno = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint32_t jobadr = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 2);
if(intno <= 0xff) {
m68k_write_memory_32(intno * 4, jobadr);
}
logcall("intno=%04x jobaddr=%08x\n", intno, jobadr);
m68k_set_reg(M68K_REG_D0, 0);
}
break;
case DOS_CALL_INTVCG: {
uint16_t intno = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
uint32_t ret = m68k_read_memory_32(intno * 4);
m68k_set_reg(M68K_REG_D0, ret);
logcall("intno=%04x ret=%08x\n", intno, ret);
}
break;
case DOS_CALL_PRNSNS: {
m68k_set_reg(M68K_REG_D0, 0);
}
break;
case DOS_CALL_CONCTRL: {
uint16_t md = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
switch(md) {
case 0x01: {
uint32_t mesptr = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 2);
uint8_t *str_ptr = &v68.ram[mesptr];
size_t l = strnlen((char *)str_ptr, v68.ram_size - mesptr);
sjis_print_utf8(str_ptr, l);
}
break;
default:
logcall("conctrl md=%d\n", md);
}
}
break;
case DOS_CALL_NAMECK: {
#define IS_SLASH(a) ((a) == '/' || (a) == '\\')
uint32_t filename = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7));
uint32_t buffer = m68k_read_memory_32(m68k_get_reg(0, M68K_REG_A7) + 4);
uint32_t len = strnlen((char *)&v68.ram[filename], v68.ram_size - filename);
/* Do some checks on the input filename */
if(v68.ram[filename] == ':' || v68.ram[filename] == '-' || v68.ram[filename] == '"' || v68.ram[filename] == '\'' || v68.ram[filename] == ',' || v68.ram[filename] == '[' || v68.ram[filename] == ']') {
m68k_set_reg(M68K_REG_D0, 0xfffffff3); // -13 Invalid file name specification
break;
}
if(buffer + 91 >= v68.ram_size) { /* Check if there's enough room for 91 bytes */
m68k_set_reg(M68K_REG_D0, 0xfffffff3); // -13 Invalid file name specification
break;
}
memset(&v68.ram[buffer], 0, 91);
int last_slash = -1;
int last_dot = -1;
/* Drive name */
int drv = v68_io_curdrv(); /* If drive is not specified, get cur drive */
if(len >= 2 && m68k_read_memory_8(filename + 1) == ':') {
drv = toupper(m68k_read_memory_8(filename)) - 'A';
last_slash = 1;
}
if(drv < 0 || drv >= 26) {
m68k_set_reg(M68K_REG_D0, 0xfffffff1); // -15 Invalid drive specification
break;
}
v68.ram[buffer + 0] = drv + 'A';
v68.ram[buffer + 1] = ':';
for(int i = 0; i < len; i++) {
if(IS_SLASH(v68.ram[filename + i])) {
last_slash = i;
}
}
for(int i = last_slash + 1; i < len; i++) {
if(v68.ram[filename + i] == '.') {
last_dot = i;
}
}
if(IS_SLASH(v68.ram[filename])) { /* Starts with a slash */
if(last_slash > 62) {
m68k_set_reg(M68K_REG_D0, 0xfffffff3); // -13 Invalid file name specification
break;
}
for(int i = 0; i < last_slash; i++) {
int c = v68.ram[filename + i];
v68.ram[buffer + i + 2] = (c == '/' ? '\\' : c);
}
} else if(v68.ram[filename + 1] == ':') {
v68.ram[buffer + 2] = '\\';
int s = IS_SLASH(v68.ram[filename + 2]) ? 3 : 2;
if(last_slash - s > 63) {
m68k_set_reg(M68K_REG_D0, 0xfffffff3); // -13 Invalid file name specification
break;
}
for(int i = 0; i < last_slash - 2; i++) {
int c = v68.ram[filename + i + s];
v68.ram[buffer + i + 3] = (c == '/' ? '\\' : c);
}
} else {
char curdir[65];
v68_io_getcwd(drv, curdir, 64);
int l = strnlen(curdir, sizeof(curdir) - 1);
if(l + last_slash > 62) {
m68k_set_reg(M68K_REG_D0, 0xfffffff3); // -13 Invalid file name specification
break;
}
v68.ram[buffer + 2] = '\\';
for(int i = 0; i < l; i++) {
int c = curdir[i];
v68.ram[buffer + 3 + i] = c == '/' ? '\\' : c;
}
v68.ram[buffer + 3 + l] = '\\';
for(int i = 0; i < last_slash; i++) {
int c = v68.ram[filename + i];
v68.ram[buffer + 4 + l + i] = c == '/' ? '\\' : c;
}
}
/* filename */
if(last_slash + 1 < len) {
int flen = len - last_slash - 1;
if(last_dot > last_slash && last_dot - last_slash < flen) flen = last_dot - last_slash - 1;
if(flen > 18) flen = 18;
memcpy(&v68.ram[buffer + 67], &v68.ram[filename + last_slash + 1], flen);
}
/* extension */
if(last_dot > -1 && last_dot + 1 < len) {
int flen = len - last_dot;
if(flen > 4) flen = 4;
memcpy(&v68.ram[buffer + 86], &v68.ram[filename + last_dot], flen);
}
}
break;
case DOS_CALL_GETPDB: {
m68k_set_reg(M68K_REG_D0, v68.cur_prog_addr - 0x10);
}
break;
case DOS_CALL_CURDRV: {
int drive = v68_io_curdrv();
m68k_set_reg(M68K_REG_D0, drive);
logcall("drive=%d (%c)\n", drive, 'A' + drive);
}
break;
case DOS_CALL_CHGDRV: {
uint16_t drive = m68k_read_memory_16(m68k_get_reg(0, M68K_REG_A7));
int curdrive = v68_io_curdrv();
v68_io_chgdrv(drive);
int num_drives = v68_io_num_drives();
m68k_set_reg(M68K_REG_D0, num_drives);
logcall("drive=%d (%c) curdrive=%d (%c) num_drives=%d\n", drive, 'A' + drive, curdrive, 'A' + curdrive, num_drives);
}
break;
default:
logcall("V68 DOS CALL %04x %s\n", instr, dos_call_names[call]);
break;
}
return 0;
}