-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.c
1986 lines (1701 loc) · 43.8 KB
/
common.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* z81/xz81, Linux console and X ZX81/ZX80 emulators.
* Copyright (C) 1994 Ian Collier. z81 changes (C) 1995-2004 Russell Marks.
* sz81 Copyright (C) 2007-2011 Thunor <thunorsif@hotmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* common.c - various routines/vars common to z81/xz81/sz81.
*/
#define Z81_VER "2.1"
#include <string.h>
#ifndef SZ81 /* Added by Thunor */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#endif
#ifdef SZ81 /* Added by Thunor */
#include "sdl.h"
#include "sdl_sound.h"
extern void sdl_set_redraw_video();
#endif
#ifndef Win32
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "common.h"
#include "sound.h"
#include "z80/z80.h"
#include "zx81.h"
#include "allmain.h"
#include "w5100.h"
extern ZX81 zx81;
#ifdef ZXMORE
unsigned char mem[0x100000];
#else
unsigned char mem[0x20000]; /* allows for shadow RAM */
#endif
unsigned char *helpscrn;
unsigned char keyports[9]={0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff};
#define SHMSIZ 0x4000
BYTE *sz81mem;
int rwsz81mem, fdsz81mem;
extern int zxmmod;
/* this two work on a per-k basis, so we can support 1k etc. properly */
unsigned char *memptr[64];
int memattr[64];
int help=0;
int sound=0;
int sound_vsync=0,sound_ay=0,sound_ay_type=AY_TYPE_NONE;
int load_hook=1,save_hook=1;
int vsync_visuals=0;
int invert_screen=0;
#ifdef SZ81 /* Added by Thunor */
int signal_int_flag=0;
#else
volatile int signal_int_flag=0;
#endif
volatile int exit_program_flag=0;
int interrupted=0;
int nmigen=0,hsyncgen=0,vsync=0;
int scrn_freq=2;
int unexpanded=0;
int taguladisp=0;
int fakedispx=0,fakedispy=0; /* set by main.c/xmain.c */
extern int ulacharline;
/* for the printer */
#ifdef SZ81 /* Added by Thunor */
/* This solves an issue for us with zxpopen. Originally the printer file
* (if requested via -p on the CLI) is created right at the start and
* it could also remain empty if nothing was printed to it, but we want
* to create a uniquely named file only when it is printed to. The issue
* with the default behaviour is that zxpopen initialises some variables
* that are used within printer_inout which is always called regardless
* of an open file, so they need to be initialised here from the outset.
* zxpopen is now called from within zxpout (for sz81) and therefore
* printer_inout gets called first.
*
* I don't know why static is being used here as it is protecting the
* variables from being modified from outside this file, but they're
* not being accessed from outside this file anyway. I think this code
* may have been copied from xz80. Also zxpframes and zxpcycles aren't
* being initialised so I'm setting them to zero too */
static unsigned long zxpframes=0, zxpcycles=0;
static int zxpspeed=0,zxpnewspeed=0;
static int zxpheight=0,zxppixel=-1,zxpstylus=0;
#else
static unsigned long zxpframes=0, zxpcycles=0;
static intzxpspeed,zxpnewspeed;
static int zxpheight,zxppixel,zxpstylus;
#endif
static FILE *zxpfile=NULL;
char *zxpfilename=NULL;
static unsigned char zxpline[256];
#ifdef SZ81 /* Added by Thunor */
/* This is actually redundant as z81's load selector has been entirely
* replaced, but a load selector component still exists within sz81 that
* is currently always off and so I'll leave this here for the moment
* and mark it temp temp */
int load_selector_state = 0;
#endif
int refresh_screen=1;
/* =1 if emulating ZX80 rather than ZX81 */
int zx80=0;
int ignore_esc=0;
int autolist=0;
int autoload=0;
char autoload_filename[1024];
int chromamode=0;
unsigned char bordercolour=0x0f;
/* not too many prototypes needed... :-) */
#ifndef SZ81 /* Added by Thunor */
char *load_selector(void);
#endif
#ifndef SZ81 /* Added by Thunor */
void sighandler(int a)
{
signal_int_flag=1;
}
#endif
#ifdef ZXMORE
char *libdir(char *file)
{
static char buf[1024];
#ifdef ZXMORE
#define LIBDIR PACKAGE_DATA_DIR
#else
#define LIBDIR ""
#endif
if(strlen(LIBDIR)+strlen(file)+2>sizeof(buf))
strcpy(buf,file); /* we know file is a short constant */
else
sprintf(buf,"%s/%s",LIBDIR,file);
return(buf);
}
#endif
#ifndef SZ81 /* Added by Thunor */
void autoload_setup(char *filename)
{
if(zx80 || (filename && strlen(filename)+1>sizeof(autoload_filename)))
return;
autoload=1;
if(filename)
strcpy(autoload_filename,filename);
else
*autoload_filename=0;
}
#endif
#ifndef SZ81 /* Added by Thunor */
static void exit_program_flag_set(int foo)
{
exit_program_flag=1;
}
#endif
#ifndef SZ81 /* Added by Thunor */
void startsigsandtimer()
{
int f,tmp=1000000/50; /* default 50 ints/sec */
struct sigaction sa;
struct itimerval itv;
int badsig[]={SIGHUP,SIGINT,SIGQUIT,SIGILL,SIGSEGV,SIGTERM,SIGBUS};
int numsig=sizeof(badsig)/sizeof(badsig[0]);
sigemptyset(&sa.sa_mask);
sa.sa_handler=exit_program_flag_set;
/* it's a bit odd using SA_RESTART for one-off exit code,
* but Solaris's SA_RESETHAND behaviour *as documented* is bogus
* (even though it seems ok in practice), so we play it safe.
*/
sa.sa_flags=SA_RESTART;
/* XXX this extra-careful masking stuff may be superfluous
* now it just sets an `exit soon' flag...
*
* handle all signals listed in badsig[], making sure that all
* signals in the list are blocked whenever handling one of them.
* There are two loops as sa_mask must be fully set before calling
* sigaction().
*/
for(f=0;f<numsig;f++)
sigaddset(&sa.sa_mask,badsig[f]);
for(f=0;f<numsig;f++)
sigaction(badsig[f],&sa,NULL);
#ifdef OSS_SOUND_SUPPORT
/* no timer signals when sound is enabled */
if(sound_enabled)
return;
#endif
sigemptyset(&sa.sa_mask); /* SIGALRM implicit */
sa.sa_handler=sighandler;
sa.sa_flags=SA_RESTART;
sigaction(SIGALRM,&sa,NULL);
itv.it_interval.tv_sec= tmp/1000000;
itv.it_interval.tv_usec=tmp-1000000*itv.it_interval.tv_sec;
itv.it_value.tv_sec=itv.it_interval.tv_sec;
itv.it_value.tv_usec=itv.it_interval.tv_usec;
setitimer(ITIMER_REAL,&itv,NULL);
}
#endif
void loadrombank(int offset)
{
memcpy(mem,sdl_zx81rom.data+offset,8192);
}
void loadrom(void)
{
#ifdef ZXMORE
FILE *in;
char *fname=libdir("ZXM-DATA.BIN");
if((in=fopen(fname,"rb"))!=NULL)
{
fread(mem,1,0x80000,in);
}
else
{
fprintf(stderr,"couldn't load ROM file %s.\n", fname);
exit(1);
}
return;
#endif
#ifdef SZ81 /* Added by Thunor */
/* sz81 has already preloaded the ROMs so now this function
* is simply copying the data into the ROM area afresh */
if(zx80)
{
memcpy(mem,sdl_zx80rom.data,sdl_zx80rom.state);
}
else
{
memcpy(mem,sdl_zx81rom.data,sdl_zx81rom.state);
}
/* fill first 16k with extra copies of it */
if (zx81.shadowROM) {
int siz=(zx80?4096:8192);
memcpy(mem+siz,mem,siz);
if(zx80)
memcpy(mem+siz*2,mem,siz*2);
}
#else
FILE *in;
char *fname=libdir(zx80?"zx80.rom":"zx81.rom");
if((in=fopen(fname,"rb"))!=NULL)
{
int siz=(zx80?4096:8192);
fread(mem,1,siz,in);
/* fill first 16k with extra copies of it
* (not really needed given since improved memptr[] resolution,
* but what the heck :-))
*/
memcpy(mem+siz,mem,siz);
if(zx80)
memcpy(mem+siz*2,mem,siz*2);
fclose(in);
}
else
{
fprintf(stderr,
"z81: couldn't load ROM file %s.\n"
" You *must* install this for z81 to work.\n",
fname);
exit(1);
}
#endif
}
/* the ZX81 char is used to index into this, to give the ascii.
* awkward chars are mapped to '_' (underscore), and the ZX81's
* all-caps is converted to lowercase.
* The mapping is also valid for the ZX80 for alphanumerics.
* WARNING: this only covers 0<=char<=63!
*/
static char zx2ascii[64]={
/* 0- 9 */ ' ', '_', '_', '_', '_', '_', '_', '_', '_', '_',
/* 10-19 */ '_', '\'','#', '$', ':', '?', '(', ')', '>', '<',
/* 20-29 */ '=', '+', '-', '*', '/', ';', ',', '.', '0', '1',
/* 30-39 */ '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
/* 40-49 */ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
/* 50-59 */ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
/* 60-63 */ 'w', 'x', 'y', 'z'
};
static char ascii2zx[96]=
{
0, 0,11,12,13, 0, 0,11,16,17,23,21,26,22,27,24,
28,29,30,31,32,33,34,35,36,37,14,25,19,20,18,15,
23,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,16,24,17,11,22,
11,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,16,24,17,11, 0
};
void kcomm(int a)
{
char buf[256], fname[256];
unsigned char *ptr=mem+a+2;
char *bptr=buf;
char *fptr=fname;
FILE *out;
int k=0;
int n=0;
unsigned int arg1, arg2;
int itxt=0;
memset(buf,0,sizeof(buf));
memset(fname,0,sizeof(fname));
do {
*bptr=zx2ascii[(*ptr)&63];
switch (k) {
case 0: if (*bptr=='\'') k++; break;
case 1: if (*bptr!='\'') *fptr++ = *bptr; else k++; break;
case 2: if (*bptr=='#') itxt=1; break;
default :;
}
bptr++;
n++;
} while((*(++ptr))!=0x76 && bptr<buf+sizeof(buf)-3);
printf("Writing files %s(.a/z)...\n",fname);
strcat(fname,".a");
if((out=fopen(fname,"wb"))==NULL) return;
fwrite(buf,n,1,out);
fclose(out);
arg1 = mem[0x4001] | (mem[0x4002] << 8);
arg2 = mem[0x4003] | (mem[0x4004] << 8);
if (itxt) { arg1 -= 2; arg2 +=1; }
n = arg2-arg1+1;
fname[strlen(fname)-1] = 'z';
if((out=fopen(fname,"wb"))==NULL) return;
fwrite(mem+arg1,n,1, out);
fclose(out);
}
unsigned int getarg1(char *buf)
{
int state=0;
int i;
int radix=10;
int n = 0;
int c;
for (i=0; i<strlen(buf); i++) {
switch (state) {
case 0:
case 1: if (buf[i]=='\'') state++; break;
case 2: if (buf[i]==' ') state=3; break;
case 3: if (buf[i]!=' ') state=4;
case 4: if (buf[i]==' ') {
state = 5;
} else if (buf[i]==':') {
radix = 16;
} else {
if (buf[i]>='a') c=buf[i]-'a'+10; else c=buf[i]-'0';
n = n*radix + c;
}
break;
default:;
}
}
return n;
}
static char lcomm_fname[256];
static FILE *lcomm_in;
static int lcomm_state=0;
static int lcomm_itxt=0;
unsigned char lcomm(int a1, int a2)
{
char buf[256];
unsigned char c=0;
unsigned char *ptr=mem+a2+1;
char *dptr=lcomm_fname;
unsigned int arg1;
switch (a1) {
case 0xf0:
memset(lcomm_fname,0,sizeof(lcomm_fname));
do
*dptr++=zx2ascii[(*ptr)&63];
while((*(++ptr))!=0x0b && dptr<lcomm_fname+sizeof(lcomm_fname)-3);
printf("Reading files %s(.a/z)...\n",lcomm_fname);
strcat(lcomm_fname,".a");
if((lcomm_in=fopen(lcomm_fname,"rb"))==NULL) return 0xff;
lcomm_state = 1;
lcomm_itxt = 0;
case 0xf1:
if (lcomm_state>2) return 0xff;
if (fread(&c,1,1,lcomm_in)!=1) {
fclose(lcomm_in);
if (lcomm_state==1) {
if (lcomm_itxt==1) {
if((lcomm_in=fopen(lcomm_fname,"rb"))==NULL) return 0xff;
c = fread(buf,sizeof(buf),1,lcomm_in);
fclose(lcomm_in);
lcomm_state++;
}
lcomm_fname[strlen(lcomm_fname)-1] = 'z';
if((lcomm_in=fopen(lcomm_fname,"rb"))==NULL) return 0xff;
if (lcomm_itxt==1) {
arg1 = getarg1(buf);
c = fread(mem+arg1,0x8000,1,lcomm_in);
fclose(lcomm_in);
lcomm_state++;
return 0x76;
}
} else {
return 0xff;
}
lcomm_state++;
} else {
if (lcomm_state==1) {
if (c=='#') lcomm_itxt++;
c = toupper(c);
c = ascii2zx[c-32];
}
}
break;
}
return c;
}
void aszmic4hacks()
{
mem[0x0690]=0xe1;
mem[0x0691]=0xef;
mem[0x0692]=0xed;
mem[0x0693]=0xfa;
mem[0x0694]=0xc9;
mem[0x06c2]=0xe1;
mem[0x06c3]=0x3e;
mem[0x06c4]=0xf0;
mem[0x06c5]=0xed;
mem[0x06c6]=0xfb;
mem[0x06c7]=0xfe;
mem[0x06c8]=0xff;
mem[0x06c9]=0xc8;
mem[0x06ca]=0xcd;
mem[0x06cb]=0x92;
mem[0x06cc]=0x04;
mem[0x06cd]=0x3e;
mem[0x06ce]=0xf1;
mem[0x06cf]=0x18;
mem[0x06d0]=0xf4;
}
void aszmic7hacks()
{
mem[0x0691]=0xe1;
mem[0x0692]=0xef;
mem[0x0693]=0xed;
mem[0x0694]=0xfa;
mem[0x0695]=0xc9;
mem[0x06c3]=0xe1;
mem[0x06c4]=0x3e;
mem[0x06c5]=0xf0;
mem[0x06c6]=0xed;
mem[0x06c7]=0xfb;
mem[0x06c8]=0xfe;
mem[0x06c9]=0xff;
mem[0x06ca]=0xc8;
mem[0x06cb]=0xcd;
mem[0x06cc]=0x90;
mem[0x06cd]=0x04;
mem[0x06ce]=0x3e;
mem[0x06cf]=0xf1;
mem[0x06d0]=0x18;
mem[0x06d1]=0xf4;
}
void lambdahacks()
{
/* patch save routine */
if(save_hook)
{
mem[0x0d0d]=0xed; mem[0x0d0e]=0xfd;
mem[0x0d0f]=0xc3; mem[0x0d10]=0x03; mem[0x0d11]=0x02;
}
/* patch load routine */
if(load_hook)
{
mem[0x19ef]=0xeb;
mem[0x19f0]=0xed; mem[0x19f1]=0xfc;
mem[0x19f2]=0xc3; mem[0x19f3]=0x03; mem[0x19f4]=0x02;
}
}
void zx80hacks()
{
#ifndef ZXPAND
#ifndef ZXNU
#ifndef VDRIVE
/* patch save routine */
if(save_hook)
{
mem[0x1b6]=0xed; mem[0x1b7]=0xfd;
mem[0x1b8]=0xc3; mem[0x1b9]=0x83; mem[0x1ba]=0x02;
}
/* patch load routine */
if(load_hook)
{
mem[0x206]=0xed; mem[0x207]=0xfc;
mem[0x208]=0xc3; mem[0x209]=0x83; mem[0x20a]=0x02;
}
#endif
#endif
#endif
}
#ifdef ZXMORE
void zx81hacks()
{
BYTE *memory;
int i;
memory = mem + 0x60000;
for (i=1; i<=6; i++) {
/* patch save routine */
if(save_hook)
{
memory[0x2fc]=0xed; memory[0x2fd]=0xfd;
memory[0x2fe]=0xc3; memory[0x2ff]=0x07; memory[0x300]=0x02;
}
/* patch load routine */
if(load_hook)
{
memory[0x347]=0xeb;
memory[0x348]=0xed; memory[0x349]=0xfc;
memory[0x34a]=0xc3; memory[0x34b]=0x07; memory[0x34c]=0x02;
}
memory -= 0x10000;
}
/* The ZXmore has a ZX80 in the first page of memory */
zx80hacks();
}
#else
void zx81hacks()
{
#ifndef ZXPAND
#ifndef ZXNU
/* patch save routine */
if(save_hook)
{
mem[0x2fc]=0xed; mem[0x2fd]=0xfd;
mem[0x2fe]=0xc3; mem[0x2ff]=0x07; mem[0x300]=0x02;
}
/* patch load routine */
if(load_hook)
{
mem[0x347]=0xeb;
mem[0x348]=0xed; mem[0x349]=0xfc;
mem[0x34a]=0xc3; mem[0x34b]=0x07; mem[0x34c]=0x02;
}
#endif
#endif
}
#endif
void initmem()
{
int f;
int ramsize;
int count;
loadrom();
#ifdef ZXMORE
memset(mem+0x80000,0xaa,0x80000); /* "clear" RAM */
#endif
#ifdef SZ81NO /* Added by Thunor */ /* Would not give shadow ROMs... */
if(zx80)
{
memset(mem+0x1000,0,0xf000);
}
else
{
memset(mem+0x2000,0,0xe000);
}
#else
memset(mem+0x4000,0,0xc000);
#endif
/* ROM setup */
count=0;
for(f=0;f<16;f++)
{
memattr[f]=memattr[32+f]=0;
memptr[f]=memptr[32+f]=mem+1024*count;
count++;
if(count>=(zx80?4:8)) count=0;
}
/* RAM setup */
#ifdef SZ81 /* Added by Thunor */
ramsize=sdl_emulator.ramsize;
#else
ramsize=16;
if(unexpanded)
ramsize=1;
#endif
count=0;
for(f=16;f<32;f++)
{
memattr[f]=memattr[32+f]=1;
memptr[f]=memptr[32+f]=mem+1024*(16+count);
count++;
if(count>=ramsize) count=0;
}
#ifdef SZ81 /* Added by Thunor */
/* z81's ROM and RAM initialisation code is OK for <= 16K RAM but beyond
* that it requires a little tweaking.
*
* The following diagram shows the ZX81 + 8K ROM. The ZX80 version is
* the same except that each 8K ROM region will contain two copies of
* the 4K ROM.
*
* RAM less than 16K is mirrored throughout the 16K region.
*
* The ROM will only detect up to 8000h when setting RAMTOP, therefore
* having more than 16K RAM will require RAMTOP to be set by the user
* (or user program) to either 49152 for 32K or 65535 for 48/56K.
*
* 1K to 16K 32K 48K 56K Extra Info.
*
* 65535 +----------+ +----------+ +----------+ +----------+
* (FFFFh) | 16K RAM | | 16K RAM | | 16K RAM | | 16K RAM | DFILE can be
* | mirrored | | mirrored | | | | | wholly here.
* | | | | | | | |
* | | | | | | | | BASIC variables
* | | | | | | | | can go here.
* 49152 +----------+ +----------+ +----------+ +----------+
* (C000h) | 8K ROM | | 16K RAM | | 16K RAM | | 16K RAM | BASIC program
* | mirrored | | | | | | | is restricted
* 40960 +----------+ | | | | | | to here.
* (A000h) | 8K ROM | | | | | | |
* | mirrored | | | | | | |
* 32768 +----------+ +----------+ +----------+ +----------+
* (8000h) | 16K RAM | | 16K RAM | | 16K RAM | | 16K RAM | No machine code
* | | | | | | | | beyond here.
* | | | | | | | |
* | | | | | | | | DFILE can be
* | | | | | | | | wholly here.
* 16384 +----------+ +----------+ +----------+ +----------+
* (4000h) | 8K ROM | | 8K ROM | | 8K ROM | | 8K RAM |
* | mirrored | | mirrored | | mirrored | | |
* 8192 +----------+ +----------+ +----------+ +----------+
* (2000h) | 8K ROM | | 8K ROM | | 8K ROM | | 8K ROM |
* | | | | | | | |
* 0 +----------+ +----------+ +----------+ +----------+
* Added: 24K option which is 16K plus 8K in 8192-16383
*/
switch(ramsize)
{
case 56:
for(f=8;f<16;f++)
{
memattr[f]=1; /* It's now writable */
memptr[f]=mem+1024*f;
}
case 48:
for(f=48;f<64;f++)
{
memattr[f]=1;
memptr[f]=mem+1024*f;
}
case 32:
for(f=32;f<48;f++)
{
memattr[f]=1;
memptr[f]=mem+1024*f;
}
break;
}
#endif
/* changed with 2.2.0 */
if(zx81.machine==MACHINEZX80)
zx80hacks();
if(zx81.machine==MACHINEZX81)
zx81hacks();
if(zx81.machine==MACHINELAMBDA)
lambdahacks();
if (sdl_emulator.networking) w_init();
/* initialise shared memory */
#ifndef Win32
if (rwsz81mem==1) {
fdsz81mem = shm_open("/sz81mem", O_RDONLY, S_IRUSR | S_IWUSR);
if (fdsz81mem < 0) {
perror("open");
exit(1);
} else {
sz81mem = mmap(NULL, SHMSIZ, PROT_READ, MAP_SHARED, fdsz81mem, 0);
if (sz81mem == MAP_FAILED) { perror("map"); exit(1); }
}
} else if (rwsz81mem==2) {
fdsz81mem = shm_open("/sz81mem", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fdsz81mem < 0) {
perror("open");
exit(1);
} else {
if (ftruncate(fdsz81mem, SHMSIZ) < 0) { perror("ftruncate"); exit(1); }
sz81mem = mmap(NULL, SHMSIZ, PROT_WRITE, MAP_SHARED, fdsz81mem, 0);
if (sz81mem == MAP_FAILED) { perror("map"); exit(1); }
}
}
#endif
}
void exitmem()
{
#ifndef Win32
if (rwsz81mem) {
if (munmap(sz81mem, SHMSIZ) < 0) perror("munmap");
if (close(fdsz81mem < 0)) perror("close");
if (rwsz81mem==2) { if (shm_unlink("/sz81mem") < 0) perror("shm_unlink"); }
}
#endif
#ifdef ZXMORE
if (zxmmod) {
FILE *out;
char *fname=libdir("ZXM-DATA.BIN");
if((out=fopen(fname,"wb"))!=NULL)
{
fwrite(mem,0x80000,1,out);
fclose(out);
}
}
#endif
}
#ifndef SZ81 /* Added by Thunor */
void loadhelp(void)
{
FILE *in;
char buf[128];
/* but first, set up location of help/selector */
fakedispx=(ZX_VID_HMARGIN-FUDGE_FACTOR)/8;
fakedispy=ZX_VID_MARGIN;
/* Thunor: Note that helpscrn below is never freed */
if((in=fopen(libdir(zx80?"zx80kybd.pbm":"zx81kybd.pbm"),"rb"))!=NULL)
{
/* ditch header lines */
fgets(buf,sizeof(buf),in);
fgets(buf,sizeof(buf),in);
if((helpscrn=calloc(256*192/8,1))==NULL)
{
fprintf(stderr,"z81: couldn't allocate memory for help screen.\n");
exit(1);
}
fread(helpscrn,1,256*192/8,in);
fclose(in);
}
else
{
fprintf(stderr,"z81: couldn't load help screen.\n");
exit(1);
}
}
#endif
// #define DEBUGPRTR
/* versus description of static values... */
void zxpinit()
{
zxpframes=0,zxpcycles=0,zxpspeed=0,zxpnewspeed=0;
zxpheight=0,zxppixel=-1,zxpstylus=0;
zxpfile=NULL;
zxpfilename=NULL;
}
void zxpopen(void)
{
#ifdef SZ81 /* Added by Thunor */
static int failcnt = 0;
#endif
if(!zxpfilename)
return;
fprintf(stdout,"Creating zxprinter file %s...\n", zxpfilename);
if((zxpfile=fopen(zxpfilename,"wb"))==NULL)
{
#ifdef SZ81 /* Added by Thunor */
if (failcnt++ < 10)
#endif
fprintf(stderr,"z81: couldn't open printer file, printing disabled\n");
return;
}
/* we reserve 10 chars for height */
fprintf(zxpfile,"P4\n256 %10d\n",0);
}
void zxpupdateheader(void)
{
long pos;
if(!zxpfile || !zxpheight) return;
pos=ftell(zxpfile);
/* seek back to write the image height */
if(fseek(zxpfile,strlen("P4\n256 "),SEEK_SET)!=0)
fprintf(stderr,"z81: warning: couldn't seek to write image height\n");
else
{
/* I originally had spaces after the image height, but that actually
* breaks the format as defined in pbm(5) (not to mention breaking
* when read by zgv :-)). So they're now before the height.
*/
fprintf(zxpfile,"%10d",zxpheight);
}
if(fseek(zxpfile,pos,SEEK_SET)!=0)
{
fprintf(stderr,"z81: error: printer file re-seek failed, printout disabled!\n");
fclose(zxpfile);
zxpfile=NULL;
}
}
void zxpclose(void)
{
int f;
/* stop the printer */
zx81_writeport(0xfb,4,0);
if(!zxpfile)
return;
/* a blank line if we haven't actually printed anything :-) */
if(!zxpheight)
{
zxpheight++;
for(f=0;f<32;f++)
fputc(0,zxpfile);
}
/* write header */
zxpupdateheader();
fclose(zxpfile);
zxpfile=NULL;
printf("Closing printer file...\n");
}
void zxpout(void)
{
int i,j,d;
if(!zxpfile)
#ifdef SZ81 /* Added by Chris */
zxpopen();
if(!zxpfile) return;
#else
return;
#endif
zxpheight++;
for(i=0;i<32;i++)
{
for(d=j=0;j<8;j++)
d=(d<<1)+(zxpline[i*8+j]?1:0);
fputc(d,zxpfile);
}
}
/* From http://problemkaputt.de/zxdocs.htm
Port FBh Read - Printer Status
Bit Expl.
0 Data Request (0=Busy, 1=Ready/DRQ)
1-5 Not used
6 Printer Detect (0=Okay, 1=None)
7 Newline (0=Nope, 1=Begin of new line)
Port FBh Write - Printer Output
Bit Expl.
0 Not used
1 Undoc/Speed? (0=Normal, 1=used to slow-down last 2 scanlines)
2 Motor (0=Start, 1=Stop)
3-6 Not used