summaryrefslogtreecommitdiffstats
path: root/apps/plugins/zxbox/snapshot.c
blob: 74845bac8eb275e5f0c78e35fdf613ff3006146b (plain)
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
/*
 * Copyright (C) 1996-1998 Szeredi Miklos
 * Email: mszeredi@inf.bme.hu
 *
 * 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. See the file COPYING. 
 *
 * 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.
 *
 */

#include <stdlib.h>
#include <string.h> /* size_t */
#include "zxmisc.h"
#include "spperif.h"
#include "z80.h"

#include "snapshot.h"
#include "compr.h"
#include "interf.h"

#include "spconf.h"

/* include this at last to avoid clashes with stdio functions */
#include "helpers.h"

#define COMPRESS_SAVE 1

static char quick_snap_file[]= ROCKBOX_DIR "/zxboxq.z80";

typedef struct {
  int isfile;
  int fd;

  unsigned len;
  byte *at;
} SNFILE;


#define sngetc(snfp) ((snfp)->isfile ? getc((snfp)->fd) : snmgetc(snfp))

static int snmgetc(SNFILE *snfp)
{
  if(!snfp->len) return EOF;
  snfp->len--;
  return *snfp->at++;
}

static int snread(void *ptr, int size, SNFILE *snfp)
{
  int i;
  byte *dest;

  if(snfp->isfile)
        return (int) rb->read( snfp->fd,ptr, (size_t) size);

  dest = (byte *) ptr;
  for(i = 0; snfp->len && size; i++, snfp->len--, size--) 
    *dest++ = *snfp->at++;

  return i;
}



/* These structures are taken from 'spconv' by Henk de Groot */

struct sna_s {
  byte i;
  byte lbk;
  byte hbk;
  byte ebk;
  byte dbk;
  byte cbk;
  byte bbk;
  byte fbk;
  byte abk;
  byte l;
  byte h;
  byte e;
  byte d;
  byte c;
  byte b;
  byte iyl;
  byte iyh;
  byte ixl;
  byte ixh;
  byte iff2;
  byte r;
  byte f;
  byte a;
  byte spl;
  byte sph;
  byte im;
  byte border;
};

#define sna_size 27        /* sizeof(struct sna_s)=27 */


struct z80_1_s {
  byte a;              /*00*/
  byte f;           /*01*/
  byte c;           /*02*/
  byte b;           /*03*/
  byte l;           /*04*/
  byte h;           /*05*/
  byte pcl;           /*06*/
  byte pch;           /*07*/
  byte spl;           /*08*/
  byte sph;           /*09*/
  byte i;           /*0A*/
  byte r;           /*0B*/
  byte data;           /*0C*/
  byte e;           /*0D*/
  byte d;           /*0E*/
  byte cbk;           /*0F*/
  byte bbk;           /*10*/
  byte ebk;           /*11*/
  byte dbk;           /*12*/
  byte lbk;           /*13*/
  byte hbk;           /*14*/
  byte abk;           /*15*/
  byte fbk;           /*16*/
  byte iyl;           /*17*/
  byte iyh;           /*18*/
  byte ixl;           /*19*/
  byte ixh;           /*1A*/
  byte iff1;           /*1B*/
  byte iff2;           /*1C*/
  byte im;           /*1D*/
};

#define z80_145_size 0x1e    /* length of z80 V1.45 header */


struct z80_2_s {
/* Extended 2.01 and 3.0 header, flagged with PC=0 */
  byte h2_len_l;       /*1E*/
  byte h2_len_h;       /*1F*/
  byte n_pcl;           /*20*/
  byte n_pch;           /*21*/
  byte hardware;       /*22*/
  byte samram;           /*23*/
  byte if1_paged;      /*24*/
  byte r_ldir_emu;     /*25*/
  byte last_out;       /*26*/
  byte sound_reg[16];  /*27*/

/* Continues with extended 3.0 header, but this part is not used anyway */
};

#define z80_201_ext_size 23    /* length of extended z80 V2.01 header */
#define z80_300_ext_size 54    /* length of extended z80 V3.0  header */


struct z80_page_s {
  byte blklen_l;       /*00*/
  byte blklen_h;       /*01*/
  byte page_num;       /*02*/
};

#define z80_pg_size 3        /* sizeof(struct z80_page_s)=3 */


static int savfd;
static int memptr;

int compr_read_byte(void)
{
  if(memptr < 0x10000) return z80_proc.mem[memptr++];
  else return -1;
}

void compr_put_byte(int i)
{
  putc(i, savfd);
}


#define STORE_NORMAL_REGS(head) \
  head.f = RF;     /* F reg */  \
  head.a = RA;     /* A reg */  \
  head.b = RB;     /* B reg */  \
  head.c = RC;     /* C reg */  \
  head.d = RD;     /* D reg */  \
  head.e = RE;     /* E reg */  \
  head.h = RH;     /* H reg */  \
  head.l = RL;     /* L reg */  \
  head.fbk = FBK;  /* F' reg */ \
  head.abk = ABK;  /* A' reg */ \
  head.bbk = BBK;  /* B' reg */ \
  head.cbk = CBK;  /* C' reg */ \
  head.dbk = DBK;  /* D' reg */ \
  head.ebk = EBK;  /* E' reg */ \
  head.hbk = HBK;  /* H' reg */ \
  head.lbk = LBK;  /* L' reg */ \
  head.iyh = YH;   /* IY reg */ \
  head.iyl = YL;                \
  head.ixh = XH;   /* IX reg */ \
  head.ixl = XL


#define LOAD_NORMAL_REGS(head) \
  RF = head.f;     /* F reg */  \
  RA = head.a;     /* A reg */  \
  RB = head.b;     /* B reg */  \
  RC = head.c;     /* C reg */  \
  RD = head.d;     /* D reg */  \
  RE = head.e;     /* E reg */  \
  RH = head.h;     /* H reg */  \
  RL = head.l;     /* L reg */  \
  FBK = head.fbk;  /* F' reg */ \
  ABK = head.abk;  /* A' reg */ \
  BBK = head.bbk;  /* B' reg */ \
  CBK = head.cbk;  /* C' reg */ \
  DBK = head.dbk;  /* D' reg */ \
  EBK = head.ebk;  /* E' reg */ \
  HBK = head.hbk;  /* H' reg */ \
  LBK = head.lbk;  /* L' reg */ \
  YH = head.iyh;   /* IY reg */ \
  YL = head.iyl;                \
  XH = head.ixh;   /* IX reg */ \
  XL = head.ixl


static void snsh_z80_save(int fd)
{
  struct z80_1_s z80;

  int to_comp = COMPRESS_SAVE;

  STORE_NORMAL_REGS(z80);

  z80.i = RI;     /* I reg */
  z80.r = RR;     /* R reg */

  z80.sph = SPH;  /* SP reg */
  z80.spl = SPL;
  z80.pch = PCH;  /* PC reg */
  z80.pcl = PCL;

  z80.iff1 = z80_proc.iff1;         /* iff1 */
  z80.iff2 = z80_proc.iff2;         /* iff2 */

  z80.im = (z80_proc.it_mode & 0x03) | 0x60;
/*
                        Bit 0-1: Interrupt mode (0, 1 or 2)
                        Bit 2  : 1=Issue 2 emulation
                        Bit 3  : 1=Double interrupt frequency
                        Bit 4-5: 1=High video synchronisation
                                 3=Low video synchronisation
                                 0,2=Normal
                        Bit 6-7: 0=Cursor/Protek/AGF joystick
                                 1=Kempston joystick
                                 2=Sinclair 1 joystick
                                 3=Sinclair 2 joystick
*/

  z80.data = ((RR >> 7) & 0x01) |
           ((z80_proc.ula_outport & 0x07) << 1) |
       (to_comp ? 0x20 : 0);
/*
                        Bit 0  : Bit 7 of the R-register
                        Bit 1-3: Border colour
                        Bit 4  : 1=Basic SamRom switched in
                        Bit 5  : 1=Block of data is compressed
                        Bit 6-7: No meaning
*/
  rb->write(fd,&z80,z80_145_size);

  if(!to_comp)
      rb->write(fd,z80_proc.mem + 0x4000,0xC000);    
  else {
    memptr = 0x4000;
    savfd = fd;
    compr();
  }
}



static void snsh_sna_save(int fd)
{
  struct sna_s sna;
  byte saves1, saves2;

  STORE_NORMAL_REGS(sna);

  sna.i = RI;     /* I reg */
  sna.r = RR;     /* R reg */

  sna.border = z80_proc.ula_outport & 0x07;

  SP -= 2;

  sna.sph = SPH;  /* SP reg */
  sna.spl = SPL;

  saves1 = z80_proc.mem[SP];
  saves2 = z80_proc.mem[(dbyte)(SP+1)]; 
  if(SP >= 0x4000) {
    z80_proc.mem[SP] = PCL;
    if(SP < 0xFFFF) z80_proc.mem[SP+1] = PCH;
  }

  sna.iff2 = z80_proc.iff2 ? 0xff : 0x00;         /* iff2 */

  sna.im = z80_proc.it_mode & 0x03;

  rb->write(fd,&sna, sna_size);
  rb->write(fd,z80_proc.mem + 0x4000, 0xC000);

  if(SP > 0x4000) {
    z80_proc.mem[SP] = saves1;
    if(SP < 0xFFFF) z80_proc.mem[SP+1] = saves2;
  }
  
  SP += 2;
}

#define GET_DATA(c) { \
  if(!datalen) break;        \
  c = sngetc(fp);             \
  if(c == EOF) break;       \
  if(datalen > 0) datalen--; \
} 


static void read_compressed_data(SNFILE *fp, byte *start, unsigned size, 
                 long datalen)
{
  int j;
  int times, last_ed, ch;
  byte *p, *end;
  
  p = start;
  end = start+size;
  last_ed = 0;
  while(p < end) {
    GET_DATA(ch);
    if(ch != 0xED) {
      last_ed = 0;
      *p++ = ch;
    }
    else {
      if(last_ed) {
    last_ed = 0;
    p--;
    GET_DATA(times);
    if(times == 0) break;

    GET_DATA(ch);
    if(p + times > end) {
      put_msg("Warning: Repeat parameter too large in snapshot");
      times = (int) ((intptr_t) end - (intptr_t) p);
    }
    for(j = 0; j < times; j++) *p++ = ch;
      }
      else {
    last_ed = 1;
    *p++ = 0xED;
      }
    }
  }

  if(datalen < 0) {
    if(sngetc(fp) != 0 || sngetc(fp) != 0xED || 
       sngetc(fp) != 0xED || sngetc(fp) != 0)
      put_msg("Warning: Illegal ending of snapshot");
  }

  if(datalen > 0) {
    while(datalen) {
      if(sngetc(fp) == EOF) break;
      datalen--;
    }
    put_msg("Warning: Page too long in snapshot");
  }

  if(p < end) put_msg("Warning: Page too short in snapshot");
}

static int read_header(void *p, int size, SNFILE *fp)
{
  int res;

  res = snread(p, size, fp);
  if(res != size) {
    put_msg("Error, End Of File in snapshot header");
    return 0;
  }
  return 1;
}

static int read_z80_page(SNFILE *fp)
{
  struct z80_page_s page;
  unsigned datalen;
  unsigned pos = 0;
  int validpage;

  int res;

  res = snread(&page, z80_pg_size, fp);
  if(res == 0) return 0;
  if(res != z80_pg_size) {
    put_msg("Error, End Of File in page header");
    return 0;
  }
  
  datalen = (page.blklen_h << 8) | page.blklen_l;
  
  validpage = 1;
  switch(page.page_num) {
  case 4:
    pos = 0x8000;
    break;

  case 5:
    pos = 0xC000;
    break;

  case 8:
    pos = 0x4000;
    break;
    
  default:
    validpage = 0;
    while(datalen) {
      if(sngetc(fp) == EOF) {
    put_msg("Warning: Page too short in snapshot");
    break;
      }
      datalen--;
    }
  }
  
  if(validpage) read_compressed_data(fp, z80_proc.mem+pos, 0x4000, 
                     (long) datalen);
  return 1;
}


static void snsh_z80_load(SNFILE *fp)
{
  struct z80_1_s z80;

  if(!read_header(&z80, z80_145_size, fp)) return;
  if(z80.pch == 0 && z80.pcl == 0) {
    struct z80_2_s z80_2;
    int ext_size, rem;
    if(!read_header(&z80_2, 2, fp)) return;
    ext_size = z80_2.h2_len_l | (z80_2.h2_len_h << 8);
    if(ext_size < z80_201_ext_size) {
      put_msg("Error in Z80 header");
      return;
    }
    if(!read_header(&z80_2.n_pcl, z80_201_ext_size, fp)) return;
    rem = ext_size - z80_201_ext_size;
    for(; rem; rem--) sngetc(fp);

    if(z80_2.hardware >= 3 && (ext_size == z80_201_ext_size || 
                z80_2.hardware >= 4)) {
      put_msg("Can't load non 48k snapshot");
      return;
    }
    if(z80_2.if1_paged) {
      put_msg("Can't load snapshot: IF1 roma paged in");
      return;
    }

    PCH = z80_2.n_pch;
    PCL = z80_2.n_pcl;

    while(read_z80_page(fp));
  }
  else {
    if(z80.data == 0xFF) z80.data = 1;
    if(z80.data & 0x20) 
      read_compressed_data(fp, z80_proc.mem + 0x4000, 0xC000, -1);
    else {
      if(snread(z80_proc.mem + 0x4000, 0xC000, fp) != 0xC000) 
    put_msg("Warning: Snapshot file too short (z80)");
      else if(sngetc(fp) != EOF) 
    put_msg("Warning: Snapshot file too long");
    }

    PCH = z80.pch;
    PCL = z80.pcl;
  }


  LOAD_NORMAL_REGS(z80);

  RI = z80.i;     /* I reg */
  RR = (z80.r & 0x7F) | ((z80.data & 0x01) << 7);     /* R reg */

  SPH = z80.sph;  /* SP reg */
  SPL = z80.spl;

  z80_proc.ula_outport = (z80_proc.ula_outport & ~(0x07)) |
    ((z80.data >> 1) & 0x07);

/*
                        Bit 0  : Bit 7 of the R-register
                        Bit 1-3: Border colour
                        Bit 4  : 1=Basic SamRom switched in
                        Bit 5  : 1=Block of data is compressed
                        Bit 6-7: No meaning
*/

  z80_proc.iff1 = z80.iff1 ? 1 : 0;
  z80_proc.iff2 = z80.iff2 ? 1 : 0;

  z80_proc.it_mode = z80.im & 0x03;

/*
                        Bit 0-1: Interrupt mode (0, 1 or 2)
                        Bit 2  : 1=Issue 2 emulation
                        Bit 3  : 1=Double interrupt frequency
                        Bit 4-5: 1=High video synchronisation
                                 3=Low video synchronisation
                                 0,2=Normal
                        Bit 6-7: 0=Cursor/Protek/AGF joystick
                                 1=Kempston joystick
                                 2=Sinclair 1 joystick
                                 3=Sinclair 2 joystick
*/

  z80_proc.haltstate = 0;

  sp_init_screen_mark();
}

static void snsh_sna_load(SNFILE *fp)
{
  struct sna_s sna;

  if(!read_header(&sna, sna_size, fp)) return;

  if(snread(z80_proc.mem+0x4000, 0xC000, fp) != 0xC000) 
    put_msg("Warning: Snapshot file too short (sna)");
  else if(sngetc(fp) != EOF) 
    put_msg("Warning: Snapshot file too long");

  LOAD_NORMAL_REGS(sna);

  RI = sna.i;     /* I reg */
  RR = sna.r;     /* R reg */

  z80_proc.ula_outport = (z80_proc.ula_outport & ~(0x07)) |
    (sna.border & 0x07);

  SPH = sna.sph;  /* SP reg */
  SPL = sna.spl;

  PCL = z80_proc.mem[SP];
  if(SP >= 0x4000) z80_proc.mem[SP] = 0;
  SP++;
  PCH = z80_proc.mem[SP];
  if(SP >= 0x4000) z80_proc.mem[SP] = 0;
  SP++;

  z80_proc.iff1 = z80_proc.iff2 = sna.iff2 ? 1 : 0;
  z80_proc.it_mode = sna.im & 0x03;
  
  z80_proc.haltstate = 0;

  sp_init_screen_mark();
}

static void save_snapshot_file_type(char *name, int type)
{
  int snsh;
  snsh = rb->open(name, O_WRONLY);
  if(snsh < 0) {
      snsh = rb->creat(name, 0666);
      if(snsh < 0) {
        put_msg("Could not create snapshot file");
        return;
      }
  }
  
  if(type == SN_SNA) snsh_sna_save(snsh);
  else if(type == SN_Z80) snsh_z80_save(snsh);

  rb->close(snsh);
}

void save_snapshot_file(char *name)
{
  int type;

  rb->strlcpy(filenamebuf, name, MAXFILENAME-10 + 1);

  type = SN_Z80;
  if(check_ext(filenamebuf, "z80")) type = SN_Z80;
  else if(check_ext(filenamebuf, "sna")) type = SN_SNA;
  else {
    add_extension(filenamebuf, "z80");
    type = SN_Z80;
  }

  save_snapshot_file_type(filenamebuf, type);
  char msgbuf [MAXFILENAME];
  rb->snprintf(msgbuf,MAXFILENAME, "Saved snapshot to file %s", filenamebuf);
  put_msg(msgbuf);
}

void save_quick_snapshot(void)
{
  save_snapshot_file_type(quick_snap_file, SN_Z80);
}

void save_snapshot(void)
{
  char name[MAXFILENAME];
  name[0]='/';
  name[1]='\0';
  put_msg("Enter name of snapshot file to save:");
  if (!rb->kbd_input((char*)&name, sizeof name))
    save_snapshot_file(&name[0]);
}


void load_snapshot_file_type(char *name, int type)
{
  int filetype = FT_SNAPSHOT;
  int snsh;
  SNFILE snfil;

  rb->strlcpy(filenamebuf, name, MAXFILENAME-10 + 1);

  spcf_find_file_type(filenamebuf, &filetype, &type);
  if(type < 0) type = SN_Z80;

  snsh = rb->open(filenamebuf, O_RDONLY);
  if(snsh < 0) {
#ifndef USE_GRAY
  rb->splashf(HZ, "Could not open snapshot file `%s'",filenamebuf);
#endif
    return;
  }
  
  snfil.isfile = 1;
  snfil.fd = snsh;

  if(type == SN_SNA) snsh_sna_load(&snfil);
  else if(type == SN_Z80) snsh_z80_load(&snfil);

  rb->close(snsh);
}

void snsh_z80_load_intern(byte *p, unsigned len)
{
  SNFILE snfil;
  
  snfil.isfile = 0;
  snfil.at = p;
  snfil.len = len;
  
  snsh_z80_load(&snfil);
}

void load_quick_snapshot(void)
{
  int  qsnap;
  qsnap = rb->open(quick_snap_file,O_RDONLY);
  if(qsnap < 0) {
    put_msg("No quick snapshot saved yet");
    return;
  }
  else
	  rb->close ( qsnap );
  load_snapshot_file_type(quick_snap_file, SN_Z80);
}


void load_snapshot(void)
{
  char *name;

  put_msg("Enter name of snapshot file to load:");

  name = spif_get_filename();
  if(name == NULL) return;

  load_snapshot_file_type(name, -1);
}