Previous 15994 Revisions Next

r7381 Tuesday 16th February, 2010 at 23:43:20 UTC by Stefano Priore
Fixed properties
[src/mess/formats]spec_snqk.c spec_snqk.h

src/mess/formats/spec_snqk.c
r7380r7381
1/***************************************************************************
2 
3  mess/formats/spec_snqk.c
4 
5  TODO:
6 
7    - Implement the following snapshot formats
8      .89C (Tezxas)
9      .FRZ (CBSpeccy)
10      .PLUSD (DISCiPLE/+D)
11      .SLT (Various emulators)
12      .SZX (Spectaculator)
13      .ZLS (ZX-Live!)
14      .ZX (ZXSpectr)
15      .ZX82 (Speculator '97)
16      .ZXS (ZX32)
17 
18    - Implement the following quickload formats
19      .$? (HoBeta)
20      .__? (SpecEm)
21      .BAS (BASin)
22      .H/.B (Roman ZX)
23      .RAW (Various emulators)
24      .SCR variants
25 
26    - Cleanup of the .Z80 format
27 
28***************************************************************************/
29 
30#include "emu.h"
31#include "cpu/z80/z80.h"
32#include "includes/spectrum.h"
33#include "eventlst.h"
34#include "video/border.h"
35#include "sound/ay8910.h"
36#include "formats/spec_snqk.h"
37#include "utils.h"
38 
39 
40/*******************************************************************
41 *
42 *      Update the memory and paging of the spectrum being emulated
43 *
44 *      if port_7ffd_data is -1 then machine is 48K - no paging
45 *      if port_1ffd_data is -1 then machine is 128K
46 *      if neither port is -1 then machine is +2a/+3
47 *
48 *      Note: the 128K .SNA and .Z80 file formats do not store the
49 *      port 1FFD setting so it is necessary to calculate the appropriate
50 *      value for the ROM paging.
51 *
52 *******************************************************************/
53static void spectrum_update_paging(running_machine *machine)
54{
55    spectrum_state *state = (spectrum_state *)machine->driver_data;
56    if (state->port_7ffd_data == -1)
57        return;
58    if (state->port_1ffd_data == -1)
59        spectrum_128_update_memory(machine);
60 
61    else
62    {
63        if (BIT(state->port_7ffd_data, 4))
64            /* Page in Spec 48K basic ROM */
65            state->port_1ffd_data = 0x04;
66        else
67            state->port_1ffd_data = 0x00;
68        spectrum_plus3_update_memory(machine);
69    }
70}
71 
72/* Page in the 48K Basic ROM. Used when running 48K snapshots on a 128K machine. */
73static void spectrum_page_basicrom(running_machine *machine)
74{
75    spectrum_state *state = (spectrum_state *)machine->driver_data;
76    if (state->port_7ffd_data == -1)
77        return;
78    state->port_7ffd_data |= 0x10;
79    spectrum_update_paging(machine);
80}
81 
82 
83SNAPSHOT_LOAD(spectrum)
84{
85    UINT8 *snapshot_data = NULL;
86 
87    snapshot_data = (UINT8*)malloc(snapshot_size);
88    if (!snapshot_data)
89        goto error;
90 
91    image_fread(image, snapshot_data, snapshot_size);
92 
93    if (!mame_stricmp(file_type, "sna"))
94    {
95        if ((snapshot_size != SNA48_SIZE) && (snapshot_size != SNA128_SIZE_1) && (snapshot_size != SNA128_SIZE_2))
96        {
97            logerror("Invalid .SNA file size.\n");
98            goto error;
99        }
100        spectrum_setup_sna(image->machine, snapshot_data, snapshot_size);
101    }
102    else if (!mame_stricmp(file_type, "sp"))
103    {
104        if ((snapshot_data[0] != 'S' && snapshot_data[1] != 'P') && (snapshot_size != SP_NEW_SIZE_16K || snapshot_size != SP_NEW_SIZE_48K))
105        {
106            if (snapshot_size != SP_OLD_SIZE)
107            {
108                logerror("Invalid .SP file size.\n");
109                goto error;
110            }
111        }
112        spectrum_setup_sp(image->machine, snapshot_data, snapshot_size);
113    }
114    else if (!mame_stricmp(file_type, "ach"))
115    {
116        if (snapshot_size != ACH_SIZE)
117        {
118            logerror("Invalid .ACH file size.\n");
119            goto error;
120        }
121        spectrum_setup_ach(image->machine, snapshot_data, snapshot_size);
122    }
123    else if (!mame_stricmp(file_type, "prg"))
124    {
125        if (snapshot_size != PRG_SIZE)
126        {
127            logerror("Invalid .PRG file size.\n");
128            goto error;
129        }
130        spectrum_setup_prg(image->machine, snapshot_data, snapshot_size);
131    }
132    else if (!mame_stricmp(file_type, "sem"))
133    {
134        if (snapshot_data[0] != 0x05 && snapshot_data[1] != 'S' && \
135            snapshot_data[2] != 'P' && snapshot_data[3] != 'E' && \
136            snapshot_data[4] != 'C' && snapshot_data[5] != '1')
137        {
138            if (snapshot_size != SEM_SIZE)
139            {
140                logerror("Invalid .SEM file size.\n");
141                goto error;
142            }
143        }
144        spectrum_setup_sem(image->machine, snapshot_data, snapshot_size);
145    }
146    else if (!mame_stricmp(file_type, "sit"))
147    {
148        if (snapshot_size != SIT_SIZE)
149        {
150            logerror("Invalid .SIT file size.\n");
151            goto error;
152        }
153        spectrum_setup_sit(image->machine, snapshot_data, snapshot_size);
154    }
155    else if (!mame_stricmp(file_type, "zx"))
156    {
157        if (snapshot_size != ZX_SIZE)
158        {
159            logerror("Invalid .ZX file size.\n");
160            goto error;
161        }
162        spectrum_setup_zx(image->machine, snapshot_data, snapshot_size);
163    }
164    else if (!mame_stricmp(file_type, "snp"))
165    {
166        if (snapshot_size != SNP_SIZE)
167        {
168            logerror("Invalid .SNP file size.\n");
169            goto error;
170        }
171        spectrum_setup_snp(image->machine, snapshot_data, snapshot_size);
172    }
173    else if (!mame_stricmp(file_type, "snx"))
174    {
175        if (snapshot_data[0] != 'X' && snapshot_data[1] != 'S' && \
176            snapshot_data[2] != 'N' && snapshot_data[3] != 'A')
177        {
178          logerror("Invalid .SEM file size.\n");
179          goto error;
180        }
181        spectrum_setup_snx(image->machine, snapshot_data, snapshot_size);
182    }
183    else
184    {
185        spectrum_setup_z80(image->machine, snapshot_data, snapshot_size);
186    }
187 
188    free(snapshot_data);
189 
190    return INIT_PASS;
191 
192error:
193    if (snapshot_data)
194        free(snapshot_data);
195    return INIT_FAIL;
196}
197 
198/*******************************************************************
199 *
200 *      Load a .SP file.
201 *
202 *      There are two kinds of .SP files: "old" and "new".
203 *
204 *      The old version is always 49184 bytes long and is created
205 *      by a leaked copy of the VGASpec emulator.
206 *
207 *      Subsequently Pedro Gimeno (the author of VGASpec) renamed it
208 *      to "Spectrum" (but it's colloquially known as the "Spanish
209 *      Spectrum emulator") and extended the header to break backward
210 *      compatibility: the new format supports both 16K and 48K
211 *      images and it's 16422 or 49190 bytes long.
212 *
213 *      http://www.formauri.es/personal/pgimeno/spec/spec.html
214 *
215 *      The formats are defined as follows:
216 *
217 *      Offset              Size    Description (all registers stored with LSB first)
218 *      ------------------- ------- -------------------------------------------------
219 *      VGASPEC SPECTRUM
220 *      ------------------- ------- -------------------------------------------------
221 *      N/A     0           2       "SP" (signature)
222 *      N/A     2           2       Program length
223 *      N/A     4           2       Program location
224 *      0       6           2       BC
225 *      2       8           2       DE
226 *      4       10          2       HL
227 *      6       12          2       AF
228 *      8       14          2       IX
229 *      10      16          2       IY
230 *      12      18          2       BC'
231 *      14      20          2       DE'
232 *      16      22          2       HL'
233 *      18      24          2       AF'
234 *      20      26          1       R
235 *      21      27          1       I
236 *      22      28          2       SP
237 *      24      30          2       PC
238 *      26      32          2       0 (reserved for future use)
239 *      28      34          1       Border color
240 *      29      35          1       0 (reserved for future use)
241 *      30      36          2       Status word
242 *      32      38          16384/  RAM dump
243 *                          49152
244 *
245 *  Status word:
246 *  Bit     Description
247 *  ------- ---------------------------------------------------
248 *  15-8    Reserved for future use
249 *  7-6     Reserved for internal use (0)
250 *  5       Status of the flash attribute: 0=normal/1=inverse
251 *  4       Interrupt pending for execution
252 *  3       If 1, IM 0; if 0, bit 1 determines interrupt mode
253 *              (Spectrum v0.99e had this behaviour reversed,
254 *              and this bit was not used in versions previous
255 *              to v 0.99e)
256 *  2       IFF2: 0=DI/1=EI
257 *  1       Interrupt mode (if bit 3 reset): 0=IM1/1=IM2
258 *  0       IFF1: 0=DI/1=EI
259 *
260 *******************************************************************/
261void spectrum_setup_sp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
262{
263    int i, SP_OFFSET;
264    UINT8 intr;
265    UINT16 start, size, data, status;
266    spectrum_state *state = (spectrum_state *)machine->driver_data;
267    running_device *cpu = devtag_get_device(machine, "maincpu");
268    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
269 
270    if (snapsize == SP_NEW_SIZE_16K || snapsize == SP_NEW_SIZE_48K)
271    {
272        SP_OFFSET = 0;
273        start = (snapdata[SP_OFFSET +  5] << 8) | snapdata[SP_OFFSET +  4];
274        size = (snapdata[SP_OFFSET +  3] << 8) | snapdata[SP_OFFSET +  2];
275    }
276    else
277    {
278        SP_OFFSET = SP_OLD_HDR - SP_NEW_HDR;
279        start = BASE_RAM;
280        size = 3*SPECTRUM_BANK;
281    }
282 
283    data = (snapdata[SP_OFFSET + 13] << 8) | snapdata[SP_OFFSET + 12];
284    LOAD_REG(cpu, Z80_AF, data);
285 
286    data = (snapdata[SP_OFFSET +  7] << 8) | snapdata[SP_OFFSET +  6];
287    LOAD_REG(cpu, Z80_BC, data);
288 
289    data = (snapdata[SP_OFFSET +  9] << 8) | snapdata[SP_OFFSET +  8];
290    LOAD_REG(cpu, Z80_DE, data);
291 
292    data = (snapdata[SP_OFFSET + 11] << 8) | snapdata[SP_OFFSET + 10];
293    LOAD_REG(cpu, Z80_HL, data);
294 
295 
296    data = (snapdata[SP_OFFSET + 25] << 8) | snapdata[SP_OFFSET + 24];
297    LOAD_REG(cpu, Z80_AF2, data);
298 
299    data = (snapdata[SP_OFFSET + 19] << 8) | snapdata[SP_OFFSET + 18];
300    LOAD_REG(cpu, Z80_BC2, data);
301 
302    data = (snapdata[SP_OFFSET + 21] << 8) | snapdata[SP_OFFSET + 20];
303    LOAD_REG(cpu, Z80_DE2, data);
304 
305    data = (snapdata[SP_OFFSET + 23] << 8) | snapdata[SP_OFFSET + 22];
306    LOAD_REG(cpu, Z80_HL2, data);
307 
308 
309    data = (snapdata[SP_OFFSET + 15] << 8) | snapdata[SP_OFFSET + 14];
310    LOAD_REG(cpu, Z80_IX, data);
311 
312    data = (snapdata[SP_OFFSET + 17] << 8) | snapdata[SP_OFFSET + 16];
313    LOAD_REG(cpu, Z80_IY, data);
314 
315 
316    data = snapdata[SP_OFFSET + 26];
317    LOAD_REG(cpu, Z80_R, data);
318 
319    data = snapdata[SP_OFFSET + 27];
320    LOAD_REG(cpu, Z80_I, data);
321 
322 
323    data = (snapdata[SP_OFFSET + 29] << 8) | snapdata[SP_OFFSET + 28];
324    LOAD_REG(cpu, Z80_SP, data);
325 
326    data = (snapdata[SP_OFFSET + 31] << 8) | snapdata[SP_OFFSET + 30];
327    LOAD_REG(cpu, Z80_PC, data);
328 
329 
330    status = (snapdata[SP_OFFSET + 37] << 8) | snapdata[SP_OFFSET + 36];
331 
332    data = (BIT(status, 3) << 1) | BIT(status, 1);
333    switch(data)
334    {
335      case 0: // case 2: in version 0.99e of the emulator
336        LOAD_REG(cpu, Z80_IM, 1);
337        break;
338      case 1: // case 3: in version 0.99e of the emulator
339        LOAD_REG(cpu, Z80_IM, 2);
340        break;
341      case 2: // case 0: in version 0.99e of the emulator
342      case 3: // case 1: in version 0.99e of the emulator
343        LOAD_REG(cpu, Z80_IM, 0);
344    }
345 
346    data = BIT(status, 0);
347    LOAD_REG(cpu, Z80_IFF1, data);
348 
349    data = BIT(status, 2);
350    LOAD_REG(cpu, Z80_IFF2, data);
351 
352    intr = BIT(status, 4) ? ASSERT_LINE : CLEAR_LINE;
353    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
354    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
355 
356    data = BIT(status, 5);
357    /* TODO: Flash bit */
358 
359    /* Memory dump */
360    logerror("Loading %04X bytes of RAM at %04X\n", size, start);
361    for (i = 0; i < size; i++)
362        memory_write_byte(space, start + i, snapdata[SP_OFFSET + SP_NEW_HDR + i]);
363 
364    /* Set border color */
365    data = snapdata[SP_OFFSET + 34] & 0x07;
366    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
367    EventList_Reset();
368    border_set_last_color(data);
369    border_force_redraw();
370    logerror("Border color:%02X\n", data);
371 
372    spectrum_page_basicrom(machine);
373 
374    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
375}
376 
377/*******************************************************************
378 *
379 *      Load a .SNA file.
380 *
381 *      This format was used by Arnt Gulbrandsen for its JPP
382 *      emulator, and it's based on the format developed for the Mirage
383 *      Microdriver, a Microdrive backup accessory.
384 *
385 *      http://www.worldofspectrum.org/infoseekid.cgi?id=1000266
386 *
387 *      The 48K image is always 49179 bytes long.
388 *
389 *      .SNA snapshots save the PC on the top of the stack, thus
390 *      deleting the original bytes: not a good feature for a snapshot...
391 *
392 *      It's been reported that snapshots saved at critical moments would
393 *      subsequently crash on restore due to the fact that the stack was full
394 *      or pointed to a ROM address. A workaround for this situation is to
395 *      store 0x0000 into the word that stored the PC.
396 *
397 *      For sake of fidelity, the code that clears the top of the stack has
398 *      been written but commented out.
399 *
400 *      Later, the format has been extended to store the whole Spectrum 128 status.
401 *
402 *      48K format as follows:
403 *
404 *      Offset  Size    Description (all registers stored with LSB first)
405 *      ------- ------- -------------------------------------------------
406 *      0       1       I
407 *      1       2       HL'
408 *      3       2       DE'
409 *      5       2       BC'
410 *      7       2       AF'
411 *      9       2       HL
412 *      11      2       DE
413 *      13      2       BC
414 *      15      2       IY
415 *      17      2       IX
416 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
417 *      20      1       R
418 *      21      2       AF
419 *      23      2       SP
420 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
421 *      26      1       Border color
422 *      27      49152   RAM dump
423 *
424 *      PC is stored on stack.
425 *
426 *      128K format as follows:
427 *
428 *      Offset  Size    Description (all registers stored with LSB first)
429 *      ------- ------- -------------------------------------------------
430 *      0       1       I
431 *      1       2       HL'
432 *      3       2       DE'
433 *      5       2       BC'
434 *      7       2       AF'
435 *      9       2       HL
436 *      11      2       DE
437 *      13      2       BC
438 *      15      2       IY
439 *      17      2       IX
440 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
441 *      20      1       R
442 *      21      2       AF
443 *      23      2       SP
444 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
445 *      26      1       Border color
446 *      27      16384   RAM bank 5 (0x4000-0x7fff)
447 *      16411   16384   RAM bank 2 (0x8000-0xbfff)
448 *      32795   16384   RAM bank n (0xc000-0xffff - currently paged bank)
449 *      49179   2       PC
450 *      49181   1       Port 7FFD data
451 *      49182   1       TR-DOS rom paged (0=no/1=yes)
452 *      49183   81920/  Remaining RAM banks in ascending order
453 *              98304
454 *
455 *      PC is NOT stored on stack.
456 *
457 *      The bank in 0xc000 is always included even if it is page 2 or 5
458 *      in which case it is included twice.
459 *
460 *******************************************************************/
461void spectrum_setup_sna(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
462{
463    int i, j, usedbanks[8];
464    long bank_offset;
465    UINT8 intr;
466    UINT16 data, addr;
467    spectrum_state *state = (spectrum_state *)machine->driver_data;
468    running_device *cpu = devtag_get_device(machine, "maincpu");
469    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
470 
471    if ((snapsize != SNA48_SIZE) && (state->port_7ffd_data == -1))
472    {
473        logerror("Can't load 128K .SNA file into 48K machine\n");
474        return;
475    }
476 
477 
478    data = (snapdata[SNA48_OFFSET + 22] << 8) | snapdata[SNA48_OFFSET + 21];
479    LOAD_REG(cpu, Z80_AF, data);
480 
481    data = (snapdata[SNA48_OFFSET + 14] << 8) | snapdata[SNA48_OFFSET + 13];
482    LOAD_REG(cpu, Z80_BC, data);
483 
484    data = (snapdata[SNA48_OFFSET + 12] << 8) | snapdata[SNA48_OFFSET + 11];
485    LOAD_REG(cpu, Z80_DE, data);
486 
487    data = (snapdata[SNA48_OFFSET + 10] << 8) | snapdata[SNA48_OFFSET +  9];
488    LOAD_REG(cpu, Z80_HL, data);
489 
490 
491    data = (snapdata[SNA48_OFFSET +  8] << 8) | snapdata[SNA48_OFFSET +  7];
492    LOAD_REG(cpu, Z80_AF2, data);
493 
494    data = (snapdata[SNA48_OFFSET +  6] << 8) | snapdata[SNA48_OFFSET +  5];
495    LOAD_REG(cpu, Z80_BC2, data);
496 
497    data = (snapdata[SNA48_OFFSET +  4] << 8) | snapdata[SNA48_OFFSET +  3];
498    LOAD_REG(cpu, Z80_DE2, data);
499 
500    data = (snapdata[SNA48_OFFSET +  2] << 8) | snapdata[SNA48_OFFSET +  1];
501    LOAD_REG(cpu, Z80_HL2, data);
502 
503 
504    data = (snapdata[SNA48_OFFSET + 18] << 8) | snapdata[SNA48_OFFSET + 17];
505    LOAD_REG(cpu, Z80_IX, data);
506 
507    data = (snapdata[SNA48_OFFSET + 16] << 8) | snapdata[SNA48_OFFSET + 15];
508    LOAD_REG(cpu, Z80_IY, data);
509 
510 
511    data = snapdata[SNA48_OFFSET + 20];
512    LOAD_REG(cpu, Z80_R, data);
513 
514    data = snapdata[SNA48_OFFSET +  0];
515    LOAD_REG(cpu, Z80_I, data);
516 
517 
518    data = (snapdata[SNA48_OFFSET + 24] << 8) | snapdata[SNA48_OFFSET + 23];
519    LOAD_REG(cpu, Z80_SP, data);
520 
521 
522    data = snapdata[SNA48_OFFSET + 25] & 0x03;
523    if (data == 3)
524        data = 2;
525    LOAD_REG(cpu, Z80_IM, data);
526 
527    data = snapdata[SNA48_OFFSET + 19];
528    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
529    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
530 
531    intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
532    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
533    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
534 
535    if (snapsize == SNA48_SIZE)
536        /* 48K Snapshot */
537        spectrum_page_basicrom(machine);
538    else
539    {
540        /* 128K Snapshot */
541        state->port_7ffd_data = snapdata[SNA128_OFFSET + 2];
542        if (snapdata[SNA128_OFFSET + 3])
543        {
544          /* TODO: page TR-DOS ROM when supported */
545        }
546        spectrum_update_paging(machine);
547    }
548 
549    if (snapsize == SNA48_SIZE)
550    {
551        /* Memory dump */
552        logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
553        for (i = 0; i < 3*SPECTRUM_BANK; i++)
554          memory_write_byte(space, BASE_RAM + i, snapdata[SNA48_HDR + i]);
555 
556        /* Get PC from stack */
557        addr = cpu_get_reg(cpu, Z80_SP);
558 
559        if (addr < BASE_RAM)
560          logerror("Corrupted SP points to ROM at:%04X", addr);
561        else
562          logerror("Fetching PC from the stack at SP:%04X\n", addr);
563 
564        data = (memory_read_byte(space, addr + 1) << 8) | memory_read_byte(space, addr + 0);
565        LOAD_REG(cpu, Z80_PC, data);
566 
567#if 0
568        memory_write_byte(space, addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
569        memory_write_byte(space, addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
570#endif
571 
572        addr += 2;
573        logerror("Fixing SP:%04X\n", addr);
574        cpu_set_reg(cpu, Z80_SP, addr);
575 
576        /* Set border color */
577        data = snapdata[SNA48_OFFSET + 26] & 0x07;
578        state->port_fe_data = (state->port_fe_data & 0xf8) | data;
579        EventList_Reset();
580        border_set_last_color(data);
581        border_force_redraw();
582        logerror("Border color:%02X\n", data);
583 
584        logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
585    }
586    else
587    {
588        /* Set up other RAM banks */
589        bank_offset = SNA48_SIZE + SNA128_OFFSET;
590        for (i = 0; i < 8; i++)
591            usedbanks[i] = 0;
592 
593        usedbanks[5] = 1;                               /* 0x4000-0x7fff */
594        usedbanks[2] = 1;                               /* 0x8000-0xbfff */
595        usedbanks[state->port_7ffd_data & 0x07] = 1;    /* Banked memory */
596 
597        logerror("Loading %05X bytes of RAM at %04X\n", 8*SPECTRUM_BANK, BASE_RAM);
598        logerror("Loading bank 5 from offset:0001B\n");
599        logerror("Loading bank 2 from offset:0401B\n");
600        logerror("Loading bank %d from offset:0801B\n", snapdata[SNA128_OFFSET + 2] & 0x07);
601        for (i = 0; i < 3*SPECTRUM_BANK; i++)
602          memory_write_byte(space, BASE_RAM + i, snapdata[SNA48_HDR + i]);
603 
604        for (i = 0; i < 8; i++)
605        {
606            if (!usedbanks[i])
607            {
608                logerror("Loading bank %d from offset:%05lX\n", i, bank_offset);
609                state->port_7ffd_data &= 0xf8;
610                state->port_7ffd_data += i;
611                spectrum_update_paging(machine);
612                for (j = 0; j < SPECTRUM_BANK; j++)
613                    memory_write_byte(space,j + 3*SPECTRUM_BANK, snapdata[bank_offset + j]);
614                bank_offset += SPECTRUM_BANK;
615            }
616        }
617 
618        /* program counter */
619        data = (snapdata[SNA128_OFFSET + 1] << 8) | snapdata[SNA128_OFFSET + 0];
620        LOAD_REG(cpu, Z80_PC, data);
621 
622        /* Set border color */
623        data = snapdata[SNA48_OFFSET + 26] & 0x07;
624        state->port_fe_data = (state->port_fe_data & 0xf8) | data;
625        EventList_Reset();
626        border_set_last_color(data);
627        border_force_redraw();
628        logerror("Border color:%02X\n", data);
629        data = state->port_7ffd_data & 0x07;
630 
631        /* Reset paging */
632        state->port_7ffd_data = snapdata[SNA128_OFFSET + 2];
633        spectrum_update_paging(machine);
634 
635        logerror("Snapshot loaded.\nExecution resuming at Bank:%d %s\n", data, cpu_get_reg_string(cpu, Z80_PC));
636    }
637}
638 
639/*******************************************************************
640 *
641 *      Load a .ACH file.
642 *
643 *      .ACH files were produced by a commercial Archimedes
644 *      (hence the name) emulator called !Speccy.
645 *
646 *      The files are always 65792 bytes long and store both
647 *      a copy of the ROM and the whole 48K RAM.
648 *
649 *
650 *      Offset  Size    Description (all registers stored with LSB first
651 *                      except when noted with *BE*)
652 *      ------- ------- -------------------------------------------------
653 *      0       4       A
654 *      4       4       F
655 *      8       4       B
656 *      12      4       C
657 *      16      4       D
658 *      20      4       E
659 *      24      4       H
660 *      28      4       L
661 *      32      4       PC
662 *      36      4       0 (reserved for future use)
663 *      40      4       SP
664 *      44      104     0 (reserved for future use)
665 *      148     4       R
666 *      152     4       0 (reserved for future use)
667 *      156     4       Border color
668 *      160     4       0 (reserved for future use)
669 *      164     2       Interrupt mode (0=IM0/1=IM1/2=IM2)
670 *      166     24      0 (reserved for future use)
671 *      190     1       I
672 *      191     1       IFF1/2: (0=DI/1=EI)
673 *      192     44      0 (reserved for future use)
674 *      236     4       AF' *BE*
675 *      240     4       BC' *BE*
676 *      244     2       DE' *BE*
677 *      246     2       HL' *BE*
678 *      248     4       IX
679 *      252     4       IY
680 *      256     16384   ROM dump
681 *      16640   49152   RAM dump
682 *
683 *******************************************************************/
684void spectrum_setup_ach(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
685{
686    int i;
687    UINT8 intr;
688    UINT16 data;
689    spectrum_state *state = (spectrum_state *)machine->driver_data;
690    running_device *cpu = devtag_get_device(machine, "maincpu");
691    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
692 
693    data = (snapdata[ACH_OFFSET +   0] << 8) | snapdata[ACH_OFFSET +   4];
694    LOAD_REG(cpu, Z80_AF, data);
695 
696    data = (snapdata[ACH_OFFSET +   8] << 8) | snapdata[ACH_OFFSET +  12];
697    LOAD_REG(cpu, Z80_BC, data);
698 
699    data = (snapdata[ACH_OFFSET +  16] << 8) | snapdata[ACH_OFFSET +  20];
700    LOAD_REG(cpu, Z80_DE, data);
701 
702    data = (snapdata[ACH_OFFSET +  24] << 8) | snapdata[ACH_OFFSET +  28];
703    LOAD_REG(cpu, Z80_HL, data);
704 
705 
706    data = (snapdata[ACH_OFFSET + 236] << 8) | snapdata[ACH_OFFSET + 237];
707    LOAD_REG(cpu, Z80_AF2, data);
708 
709    data = (snapdata[ACH_OFFSET + 240] << 8) | snapdata[ACH_OFFSET + 241];
710    LOAD_REG(cpu, Z80_BC2, data);
711 
712    data = (snapdata[ACH_OFFSET + 244] << 8) | snapdata[ACH_OFFSET + 245];
713    LOAD_REG(cpu, Z80_DE2, data);
714 
715    data = (snapdata[ACH_OFFSET + 246] << 8) | snapdata[ACH_OFFSET + 247];
716    LOAD_REG(cpu, Z80_HL2, data);
717 
718 
719    data = (snapdata[ACH_OFFSET + 249] << 8) | snapdata[ACH_OFFSET + 248];
720    LOAD_REG(cpu, Z80_IX, data);
721 
722    data = (snapdata[ACH_OFFSET + 253] << 8) | snapdata[ACH_OFFSET + 252];
723    LOAD_REG(cpu, Z80_IY, data);
724 
725    data = snapdata[ACH_OFFSET + 148];
726    LOAD_REG(cpu, Z80_R, data);
727 
728    data = snapdata[ACH_OFFSET + 190];
729    LOAD_REG(cpu, Z80_I, data);
730 
731 
732    data = (snapdata[ACH_OFFSET +  41] << 8) | snapdata[ACH_OFFSET +  40];
733    LOAD_REG(cpu, Z80_SP, data);
734 
735    data = (snapdata[ACH_OFFSET +  33] << 8) | snapdata[ACH_OFFSET +  32];
736    LOAD_REG(cpu, Z80_PC, data);
737 
738    data = snapdata[ACH_OFFSET + 164] & 0x03;
739    if (data == 3)
740        data = 0;
741    LOAD_REG(cpu, Z80_IM, data);
742 
743    data = snapdata[ACH_OFFSET + 191] ? 1 : 0;
744    LOAD_REG(cpu, Z80_IFF1, data);
745    LOAD_REG(cpu, Z80_IFF2, data);
746 
747    intr = snapdata[ACH_OFFSET + 191] ? CLEAR_LINE : ASSERT_LINE;
748    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
749    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
750 
751    logerror("Skipping the 16K ROM dump at offset:%04X\n", ACH_OFFSET + 256);
752 
753    /* Memory dump */
754    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
755    for (i = 0; i < 3*SPECTRUM_BANK; i++)
756        memory_write_byte(space, BASE_RAM + i, snapdata[ACH_HDR + SPECTRUM_BANK + i]);
757 
758    /* Set border color */
759    data = snapdata[ACH_OFFSET + 156] & 0x07;
760    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
761    EventList_Reset();
762    border_set_last_color(data);
763    border_force_redraw();
764    logerror("Border color:%02X\n", data);
765 
766    spectrum_page_basicrom(machine);
767 
768    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
769}
770 
771/*******************************************************************
772 *
773 *      Load a .PRG file.
774 *
775 *      .PRG files were produced by an irish emulator called SpecEm
776 *      written by Kevin Phair (thanks for the help, Kevin!)
777 *
778 *      http://code.google.com/p/specem/
779 *
780 *      The files are always 49408 bytes long and use some stack space
781 *      to store registers.
782 *
783 *      The header actually mimics the file descriptor format used by
784 *      the DISCiPLE/+D interface.
785 *
786 *      Offset  Size    Description (all registers stored with LSB first
787 *                      except when noted with *BE*)
788 *      ------- ------- -------------------------------------------------
789 *      0       1       File type (always 0x05, corresponding to "48K Snapshot"
790 *      1       10      Program name (padded with 0x20)
791 *      11      2       Number of sectors occupied by the file *BE*
792 *      13      1       Track number of the first sector of the file
793 *      14      1       Sector number of the first sector of the file
794 *      15      195     Sector allocation bitmap
795 *      210     10      0 (reserved for future use)
796 *      220     2       IY
797 *      222     2       IX
798 *      224     2       DE'
799 *      226     2       BC'
800 *      228     2       HL'
801 *      230     2       AF'
802 *      232     2       DE
803 *      234     2       BC
804 *      236     2       HL
805 *      238     1       IFF1/2: (bit 2, 0=DI/1=EI)
806 *      239     1       I
807 *      240     2       SP
808 *      242     14      0 (reserved for future use)
809 *      256     49152   RAM dump
810 *
811 *      R,AF and PC are stored on the stack. Due to a bug in the
812 *      BIOS, the snapshots created with the +D have the AF' register
813 *      replaced with the AF register.
814 *
815 *      It's unknown (but likely possible) that the snapshots could
816 *      suffer from the same "top of the stack" bug as well as .SNA images.
817 *
818 *******************************************************************/
819void spectrum_setup_prg(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
820{
821    int i;
822    UINT8 intr;
823    UINT16 addr, data;
824    spectrum_state *state = (spectrum_state *)machine->driver_data;
825    running_device *cpu = devtag_get_device(machine, "maincpu");
826    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
827 
828    data = snapdata[PRG_OFFSET +   0];
829    if (data != 0x05)
830      logerror("Wrong DISCiPLE/+D file type: %02X instead of 05\n", data);
831 
832    data = (snapdata[PRG_OFFSET + 235] << 8) | snapdata[PRG_OFFSET + 234];
833    LOAD_REG(cpu, Z80_BC, data);
834 
835    data = (snapdata[PRG_OFFSET + 233] << 8) | snapdata[PRG_OFFSET + 232];
836    LOAD_REG(cpu, Z80_DE, data);
837 
838    data = (snapdata[PRG_OFFSET + 237] << 8) | snapdata[PRG_OFFSET + 236];
839    LOAD_REG(cpu, Z80_HL, data);
840 
841 
842    data = (snapdata[PRG_OFFSET + 231] << 8) | snapdata[PRG_OFFSET + 230];
843    LOAD_REG(cpu, Z80_AF2, data);
844 
845    data = (snapdata[PRG_OFFSET + 227] << 8) | snapdata[PRG_OFFSET + 226];
846    LOAD_REG(cpu, Z80_BC2, data);
847 
848    data = (snapdata[PRG_OFFSET + 225] << 8) | snapdata[PRG_OFFSET + 224];
849    LOAD_REG(cpu, Z80_DE2, data);
850 
851    data = (snapdata[PRG_OFFSET + 229] << 8) | snapdata[PRG_OFFSET + 228];
852    LOAD_REG(cpu, Z80_HL2, data);
853 
854 
855    data = (snapdata[PRG_OFFSET + 223] << 8) | snapdata[PRG_OFFSET + 222];
856    LOAD_REG(cpu, Z80_IX, data);
857 
858    data = (snapdata[PRG_OFFSET + 221] << 8) | snapdata[PRG_OFFSET + 220];
859    LOAD_REG(cpu, Z80_IY, data);
860 
861    data = snapdata[PRG_OFFSET + 239];
862    LOAD_REG(cpu, Z80_I, data);
863 
864    LOAD_REG(cpu, Z80_IM, (data == 0x00 || data == 0x3f) ? 1 : 2);
865 
866    data = snapdata[PRG_OFFSET + 238];
867    LOAD_REG(cpu, Z80_IFF1, BIT(data, 2));
868    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
869 
870    intr = BIT(data, 2) ? CLEAR_LINE : ASSERT_LINE;
871    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
872    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
873 
874    /* Memory dump */
875    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
876    for (i = 0; i < 3*SPECTRUM_BANK; i++)
877        memory_write_byte(space, BASE_RAM + i, snapdata[PRG_HDR + i]);
878 
879    addr = (snapdata[PRG_OFFSET + 241] << 8) | snapdata[PRG_OFFSET + 240];
880    if (addr < BASE_RAM)
881      logerror("Corrupted SP points to ROM at:%04X", addr);
882    else
883      logerror("Fetching registers R, AF and PC from the stack at SP:%04X\n", addr);
884 
885    data = memory_read_byte(space, addr + 0); // Junk created when pushing R on the stack via LD A,R/PUSH AF
886 
887    data = memory_read_byte(space, addr + 1);
888    LOAD_REG(cpu, Z80_R, data);
889 
890    data = (memory_read_byte(space, addr + 3) << 8) | memory_read_byte(space, addr + 2);
891    LOAD_REG(cpu, Z80_AF, data);
892 
893    data = (memory_read_byte(space, addr + 5) << 8) | memory_read_byte(space, addr + 4);
894    LOAD_REG(cpu, Z80_PC, data);
895 
896    addr += 6;
897    logerror("Fixing SP:%04X\n", addr);
898    cpu_set_reg(cpu, Z80_SP, addr);
899 
900    /* Set border color */
901    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
902    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
903    EventList_Reset();
904    border_set_last_color(data);
905    border_force_redraw();
906    logerror("Border color:%02X\n", data);
907 
908    spectrum_page_basicrom(machine);
909 
910    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
911}
912 
913/*******************************************************************
914 *
915 *      Load a .SEM file.
916 *
917 *      .SEM files were produced by a german emulator called SpecEmu
918 *      written by Bernd Waschke.
919 *
920 *      The files are usually 49192 bytes long unless optional POKE
921 *      blocks are stored at the end of the file.
922 *
923 *      Offset  Size    Description (all registers stored with LSB first)
924 *      ------- ------- -------------------------------------------------
925 *      0       1       0x05 (signature length)
926 *      1       5       "SPEC1" (signature)
927 *      6       49152   RAM dump
928 *      49158   2       AF
929 *      49160   2       BC
930 *      49162   2       DE
931 *      49164   2       HL
932 *      49166   2       AF'
933 *      49168   2       BC'
934 *      49170   2       DE'
935 *      49172   2       HL'
936 *      49174   2       PC
937 *      49176   2       SP
938 *      49178   2       IX
939 *      49180   2       IY
940 *      49182   2       I
941 *      49184   2       R
942 *      49186   2       IFF1 (0=DI/1=EI)
943 *      49188   2       IFF2 (0=DI/1=EI)
944 *      49190   2       Interrupt mode (0=IM0/1=IM1/2=IM2)
945 *
946 *      Following these data, there are optional POKE blocks
947 *
948 *******************************************************************/
949void spectrum_setup_sem(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
950{
951    int i;
952    UINT8 intr;
953    UINT16 data;
954    spectrum_state *state = (spectrum_state *)machine->driver_data;
955    running_device *cpu = devtag_get_device(machine, "maincpu");
956    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
957 
958    data = (snapdata[SEM_OFFSET +  1] << 8) | snapdata[SEM_OFFSET +  0];
959    LOAD_REG(cpu, Z80_AF, data);
960 
961    data = (snapdata[SEM_OFFSET +  3] << 8) | snapdata[SEM_OFFSET +  2];
962    LOAD_REG(cpu, Z80_BC, data);
963 
964    data = (snapdata[SEM_OFFSET +  5] << 8) | snapdata[SEM_OFFSET +  4];
965    LOAD_REG(cpu, Z80_DE, data);
966 
967    data = (snapdata[SEM_OFFSET +  7] << 8) | snapdata[SEM_OFFSET +  6];
968    LOAD_REG(cpu, Z80_HL, data);
969 
970 
971    data = (snapdata[SEM_OFFSET +  9] << 8) | snapdata[SEM_OFFSET +  8];
972    LOAD_REG(cpu, Z80_AF2, data);
973 
974    data = (snapdata[SEM_OFFSET + 11] << 8) | snapdata[SEM_OFFSET + 10];
975    LOAD_REG(cpu, Z80_BC2, data);
976 
977    data = (snapdata[SEM_OFFSET + 13] << 8) | snapdata[SEM_OFFSET + 12];
978    LOAD_REG(cpu, Z80_DE2, data);
979 
980    data = (snapdata[SEM_OFFSET + 15] << 8) | snapdata[SEM_OFFSET + 14];
981    LOAD_REG(cpu, Z80_HL2, data);
982 
983 
984    data = (snapdata[SEM_OFFSET + 21] << 8) | snapdata[SEM_OFFSET + 20];
985    LOAD_REG(cpu, Z80_IX, data);
986 
987    data = (snapdata[SEM_OFFSET + 23] << 8) | snapdata[SEM_OFFSET + 22];
988    LOAD_REG(cpu, Z80_IY, data);
989 
990 
991    data = snapdata[SEM_OFFSET + 26];
992    LOAD_REG(cpu, Z80_R, data);
993 
994    data = snapdata[SEM_OFFSET + 24];
995    LOAD_REG(cpu, Z80_I, data);
996 
997 
998    data = (snapdata[SEM_OFFSET + 19] << 8) | snapdata[SEM_OFFSET + 18];
999    LOAD_REG(cpu, Z80_SP, data);
1000 
1001    data = (snapdata[SEM_OFFSET + 17] << 8) | snapdata[SEM_OFFSET + 16];
1002    LOAD_REG(cpu, Z80_PC, data);
1003 
1004    data = snapdata[SEM_OFFSET + 32];
1005    LOAD_REG(cpu, Z80_IM, data);
1006 
1007    data = snapdata[SEM_OFFSET + 28];
1008    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
1009    data = snapdata[SEM_OFFSET + 30];
1010    LOAD_REG(cpu, Z80_IFF2, BIT(data, 0));
1011 
1012    intr = BIT(snapdata[SEM_OFFSET + 30], 0) ? CLEAR_LINE : ASSERT_LINE;
1013    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1014    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1015 
1016    /* Memory dump */
1017    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1018    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1019        memory_write_byte(space, BASE_RAM + i, snapdata[SEM_SIGNATURE + i]);
1020 
1021    /* Set border color */
1022    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
1023    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1024    EventList_Reset();
1025    border_set_last_color(data);
1026    border_force_redraw();
1027    logerror("Border color:%02X\n", data);
1028 
1029    spectrum_page_basicrom(machine);
1030 
1031    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1032 
1033/* TODO: Decode the optional POKE bank at the end of the image */
1034 
1035}
1036 
1037/*******************************************************************
1038 *
1039 *      Load a .SIT file.
1040 *
1041 *      .SIT files were produced by a spanish emulator called Sinclair
1042 *      written by Pedro M. R. Salas.
1043 *
1044 *      http://www.ugr.es/~pedrom/sinclair.htm
1045 *
1046 *      The files are always 65564 bytes long and store both ROM and RAM.
1047 *
1048 *      Offset  Size    Description (all registers stored with LSB first)
1049 *      ------- ------- -------------------------------------------------
1050 *      0       2       BC
1051 *      2       2       DE
1052 *      4       2       HL
1053 *      6       2       AF
1054 *      8       2       IX
1055 *      10      2       IY
1056 *      12      2       SP
1057 *      14      2       PC
1058 *      16      1       R
1059 *      17      1       I
1060 *      18      2       BC'
1061 *      20      2       DE'
1062 *      22      2       HL'
1063 *      24      2       AF'
1064 *      26      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
1065 *      27      1       Border color
1066 *      28      16384   ROM dump
1067 *      16412   49152   RAM dump
1068 *
1069 *******************************************************************/
1070void spectrum_setup_sit(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1071{
1072    int i;
1073    UINT8 intr;
1074    UINT16 data;
1075    spectrum_state *state = (spectrum_state *)machine->driver_data;
1076    running_device *cpu = devtag_get_device(machine, "maincpu");
1077    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1078 
1079    data = (snapdata[SIT_OFFSET +  7] << 8) | snapdata[SIT_OFFSET +  6];
1080    LOAD_REG(cpu, Z80_AF, data);
1081 
1082    data = (snapdata[SIT_OFFSET +  1] << 8) | snapdata[SIT_OFFSET +  0];
1083    LOAD_REG(cpu, Z80_BC, data);
1084 
1085    data = (snapdata[SIT_OFFSET +  3] << 8) | snapdata[SIT_OFFSET +  2];
1086    LOAD_REG(cpu, Z80_DE, data);
1087 
1088    data = (snapdata[SIT_OFFSET +  5] << 8) | snapdata[SIT_OFFSET +  4];
1089    LOAD_REG(cpu, Z80_HL, data);
1090 
1091 
1092    data = (snapdata[SIT_OFFSET + 25] << 8) | snapdata[SIT_OFFSET + 24];
1093    LOAD_REG(cpu, Z80_AF2, data);
1094 
1095    data = (snapdata[SIT_OFFSET + 19] << 8) | snapdata[SIT_OFFSET + 18];
1096    LOAD_REG(cpu, Z80_BC2, data);
1097 
1098    data = (snapdata[SIT_OFFSET + 21] << 8) | snapdata[SIT_OFFSET + 20];
1099    LOAD_REG(cpu, Z80_DE2, data);
1100 
1101    data = (snapdata[SIT_OFFSET + 23] << 8) | snapdata[SIT_OFFSET + 22];
1102    LOAD_REG(cpu, Z80_HL2, data);
1103 
1104 
1105    data = (snapdata[SIT_OFFSET +  9] << 8) | snapdata[SIT_OFFSET +  8];
1106    LOAD_REG(cpu, Z80_IX, data);
1107 
1108    data = (snapdata[SIT_OFFSET + 11] << 8) | snapdata[SIT_OFFSET + 10];
1109    LOAD_REG(cpu, Z80_IY, data);
1110 
1111 
1112    data = snapdata[SIT_OFFSET + 16];
1113    LOAD_REG(cpu, Z80_R, data);
1114 
1115    data = snapdata[SIT_OFFSET + 17];
1116    LOAD_REG(cpu, Z80_I, data);
1117 
1118 
1119    data = (snapdata[SIT_OFFSET + 13] << 8) | snapdata[SIT_OFFSET + 12];
1120    LOAD_REG(cpu, Z80_SP, data);
1121 
1122    data = (snapdata[SIT_OFFSET + 15] << 8) | snapdata[SIT_OFFSET + 14];
1123    LOAD_REG(cpu, Z80_PC, data);
1124 
1125    data = snapdata[SIT_OFFSET + 26];
1126    LOAD_REG(cpu, Z80_IM, data);
1127 
1128    data = 0x01; // .SIT snapshots don't specify whether interrupts are enabled or not, so I assume they are.
1129    LOAD_REG(cpu, Z80_IFF1, data);
1130    LOAD_REG(cpu, Z80_IFF2, data);
1131 
1132    intr = data ? CLEAR_LINE : ASSERT_LINE;
1133    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1134    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1135 
1136    /* Memory dump */
1137    logerror("Skipping the 16K ROM dump at offset:%04X\n", SIT_OFFSET + 28);
1138    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1139    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1140        memory_write_byte(space, BASE_RAM + i, snapdata[SIT_HDR + SPECTRUM_BANK + i]);
1141 
1142    /* Set border color */
1143    data = snapdata[SIT_OFFSET + 27] & 0x07;
1144    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1145    EventList_Reset();
1146    border_set_last_color(data);
1147    border_force_redraw();
1148    logerror("Border color:%02X\n", data);
1149 
1150    spectrum_page_basicrom(machine);
1151 
1152    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1153}
1154 
1155/*******************************************************************
1156 *
1157 *      Load a .ZX file.
1158 *
1159 *      .ZX files were produced by the Amiga emulator called KGB
1160 *
1161 *      The files are usually 49486 bytes and store at the beginning
1162 *      of the file the last 132 bytes of the ROM.
1163 *
1164 *      Offset  Size    Description (all registers stored with MSB first)
1165 *      ------- ------- -------------------------------------------------
1166 *      0       132     Last 132 bytes of ROM dump
1167 *      132     49152   RAM dump
1168 *      49284   132     0 (reserved for future use)
1169 *      49416   10      Various KGB settings
1170 *      49426   1       IFF1/2: (0=DI/1=EI)
1171 *      49427   2       Reserved (must be 0x0003)
1172 *      49429   1       KGB ColorMode (0=BW/1=Color)
1173 *      49430   4       0 (reserved for future use)
1174 *      49434   2       BC
1175 *      49436   2       BC'
1176 *      49438   2       DE
1177 *      49440   2       DE'
1178 *      49442   2       HL
1179 *      49444   2       HL'
1180 *      49446   2       IX
1181 *      49448   2       IY
1182 *      49450   1       I
1183 *      49451   1       R
1184 *      49452   3       0 (reserved for future use)
1185 *      49455   1       A'
1186 *      49456   1       0 (reserved for future use)
1187 *      49457   1       A
1188 *      49458   1       0 (reserved for future use)
1189 *      49459   1       F'
1190 *      49460   1       0 (reserved for future use)
1191 *      49461   1       F
1192 *      49462   2       0 (reserved for future use)
1193 *      49464   2       PC
1194 *      49466   2       0 (reserved for future use)
1195 *      49468   2       SP
1196 *      49470   2       KGB Soundmode (0=Simple/1=Pitch/2=RomOnly)
1197 *      49472   2       KGB HaltMode (0=NoHalt/1=Halt)
1198 *      49474   2       Interrupt mode (-1=IM0/0=IM1/1=IM2)
1199 *      49476   10      0 (reserved for future use)
1200 *
1201 *******************************************************************/
1202void spectrum_setup_zx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1203{
1204    int i;
1205    UINT8 intr;
1206    UINT16 data, mode;
1207    spectrum_state *state = (spectrum_state *)machine->driver_data;
1208    running_device *cpu = devtag_get_device(machine, "maincpu");
1209    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1210 
1211    logerror("Skipping last 132 bytes of the 16K ROM dump at offset:0000\n");
1212 
1213    data = (snapdata[ZX_OFFSET + 173] << 8) | snapdata[ZX_OFFSET + 177];
1214    LOAD_REG(cpu, Z80_AF, data);
1215 
1216    data = (snapdata[ZX_OFFSET + 150] << 8) | snapdata[ZX_OFFSET + 151];
1217    LOAD_REG(cpu, Z80_BC, data);
1218 
1219    data = (snapdata[ZX_OFFSET + 154] << 8) | snapdata[ZX_OFFSET + 155];
1220    LOAD_REG(cpu, Z80_DE, data);
1221 
1222    data = (snapdata[ZX_OFFSET + 158] << 8) | snapdata[ZX_OFFSET + 159];
1223    LOAD_REG(cpu, Z80_HL, data);
1224 
1225 
1226    data = (snapdata[ZX_OFFSET + 171] << 8) | snapdata[ZX_OFFSET + 175];
1227    LOAD_REG(cpu, Z80_AF2, data);
1228 
1229    data = (snapdata[ZX_OFFSET + 152] << 8) | snapdata[ZX_OFFSET + 153];
1230    LOAD_REG(cpu, Z80_BC2, data);
1231 
1232    data = (snapdata[ZX_OFFSET + 156] << 8) | snapdata[ZX_OFFSET + 157];
1233    LOAD_REG(cpu, Z80_DE2, data);
1234 
1235    data = (snapdata[ZX_OFFSET + 160] << 8) | snapdata[ZX_OFFSET + 161];
1236    LOAD_REG(cpu, Z80_HL2, data);
1237 
1238 
1239    data = (snapdata[ZX_OFFSET + 162] << 8) | snapdata[ZX_OFFSET + 163];
1240    LOAD_REG(cpu, Z80_IX, data);
1241 
1242    data = (snapdata[ZX_OFFSET + 164] << 8) | snapdata[ZX_OFFSET + 165];
1243    LOAD_REG(cpu, Z80_IY, data);
1244 
1245 
1246    data = snapdata[ZX_OFFSET + 167];
1247    LOAD_REG(cpu, Z80_R, data);
1248 
1249    data = snapdata[ZX_OFFSET + 166];
1250    LOAD_REG(cpu, Z80_I, data);
1251 
1252 
1253    data = (snapdata[ZX_OFFSET + 184] << 8) | snapdata[ZX_OFFSET + 185];
1254    LOAD_REG(cpu, Z80_SP, data);
1255 
1256    data = (snapdata[ZX_OFFSET + 180] << 8) | snapdata[ZX_OFFSET + 181];
1257    LOAD_REG(cpu, Z80_PC, data);
1258 
1259    mode = (snapdata[ZX_OFFSET + 190] << 8) | snapdata[ZX_OFFSET + 191];
1260    switch (mode)
1261    {
1262      case 0xffff:
1263        LOAD_REG(cpu, Z80_IM, 0);
1264        break;
1265      case 0x00:
1266        LOAD_REG(cpu, Z80_IM, 1);
1267        break;
1268      case 0x01:
1269        LOAD_REG(cpu, Z80_IM, 2);
1270        break;
1271      default:
1272        logerror("Invalid IM:%04X\n", mode);
1273    }
1274 
1275    data = BIT(snapdata[ZX_OFFSET + 142], 0);
1276    LOAD_REG(cpu, Z80_IFF1, data);
1277    LOAD_REG(cpu, Z80_IFF2, data);
1278 
1279    intr = BIT(snapdata[ZX_OFFSET + 142], 0) ? CLEAR_LINE : ASSERT_LINE;
1280    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1281    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1282 
1283    /* Memory dump */
1284    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1285    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1286        memory_write_byte(space, BASE_RAM + i, snapdata[132 + i]);
1287 
1288    /* Set border color */
1289    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
1290    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1291    EventList_Reset();
1292    border_set_last_color(data);
1293    border_force_redraw();
1294    logerror("Border color:%02X\n", data);
1295 
1296    spectrum_page_basicrom(machine);
1297 
1298    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1299}
1300 
1301/*******************************************************************
1302 *
1303 *      Load a .SNP file.
1304 *
1305 *      .SNP files were produced by a polish emulator called Nuclear ZX
1306 *      written by Radovan Garabik and Lubomir Salanci.
1307 *
1308 *      http://korpus.juls.savba.sk/~garabik/old/zx.html
1309 *
1310 *      The files are always 49183 bytes long.
1311 *
1312 *      Offset  Size    Description (all registers stored with LSB first)
1313 *      ------- ------- -------------------------------------------------
1314 *      0       49152   RAM dump
1315 *      49152   2       AF
1316 *      49154   1       Border color
1317 *      49155   1       0 (reserved for future use)
1318 *      49156   2       BC
1319 *      49158   2       DE
1320 *      49160   2       HL
1321 *      49162   2       PC
1322 *      49164   2       SP
1323 *      49166   2       IX
1324 *      49168   2       IY
1325 *      49170   1       0 (reserved for IFF2 but not implemented)
1326 *      49171   1       IFF1 (0=DI/other=EI)
1327 *      49172   1       Interrupt mode (0=IM0/1=IM1/2=IM2)
1328 *      49173   1       R
1329 *      49174   1       I
1330 *      49175   2       AF'
1331 *      49177   2       BC'
1332 *      49179   2       DE'
1333 *      49181   2       HL'
1334 *
1335 *******************************************************************/
1336void spectrum_setup_snp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1337{
1338    int i;
1339    UINT8 intr;
1340    UINT16 data;
1341    spectrum_state *state = (spectrum_state *)machine->driver_data;
1342    running_device *cpu = devtag_get_device(machine, "maincpu");
1343    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1344 
1345    data = (snapdata[SNP_OFFSET +  1] << 8) | snapdata[SNP_OFFSET +  0];
1346    LOAD_REG(cpu, Z80_AF, data);
1347 
1348    data = (snapdata[SNP_OFFSET +  5] << 8) | snapdata[SNP_OFFSET +  4];
1349    LOAD_REG(cpu, Z80_BC, data);
1350 
1351    data = (snapdata[SNP_OFFSET +  7] << 8) | snapdata[SNP_OFFSET +  6];
1352    LOAD_REG(cpu, Z80_DE, data);
1353 
1354    data = (snapdata[SNP_OFFSET +  9] << 8) | snapdata[SNP_OFFSET +  8];
1355    LOAD_REG(cpu, Z80_HL, data);
1356 
1357 
1358    data = (snapdata[SNP_OFFSET + 24] << 8) | snapdata[SNP_OFFSET + 23];
1359    LOAD_REG(cpu, Z80_AF2, data);
1360 
1361    data = (snapdata[SNP_OFFSET + 26] << 8) | snapdata[SNP_OFFSET + 25];
1362    LOAD_REG(cpu, Z80_BC2, data);
1363 
1364    data = (snapdata[SNP_OFFSET + 28] << 8) | snapdata[SNP_OFFSET + 27];
1365    LOAD_REG(cpu, Z80_DE2, data);
1366 
1367    data = (snapdata[SNP_OFFSET + 30] << 8) | snapdata[SNP_OFFSET + 29];
1368    LOAD_REG(cpu, Z80_HL2, data);
1369 
1370 
1371    data = (snapdata[SNP_OFFSET + 15] << 8) | snapdata[SNP_OFFSET + 14];
1372    LOAD_REG(cpu, Z80_IX, data);
1373 
1374    data = (snapdata[SNP_OFFSET + 17] << 8) | snapdata[SNP_OFFSET + 16];
1375    LOAD_REG(cpu, Z80_IY, data);
1376 
1377 
1378    data = snapdata[SNP_OFFSET + 21];
1379    LOAD_REG(cpu, Z80_R, data);
1380 
1381    data = snapdata[SNP_OFFSET + 22];
1382    LOAD_REG(cpu, Z80_I, data);
1383 
1384 
1385    data = (snapdata[SNP_OFFSET + 13] << 8) | snapdata[SNP_OFFSET + 12];
1386    LOAD_REG(cpu, Z80_SP, data);
1387 
1388    data = (snapdata[SNP_OFFSET + 11] << 8) | snapdata[SNP_OFFSET + 10];
1389    LOAD_REG(cpu, Z80_PC, data);
1390 
1391 
1392    data = snapdata[SNP_OFFSET + 91] & 0x03;
1393    LOAD_REG(cpu, Z80_IM, data);
1394 
1395    data = BIT(snapdata[SNP_OFFSET + 19], 0);
1396    LOAD_REG(cpu, Z80_IFF1, data);
1397    LOAD_REG(cpu, Z80_IFF2, data);
1398 
1399    intr = BIT(snapdata[SNP_OFFSET + 19], 0) ? CLEAR_LINE : ASSERT_LINE;
1400    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1401    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1402 
1403    /* Memory dump */
1404    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1405    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1406        memory_write_byte(space, BASE_RAM + i, snapdata[i]);
1407 
1408    /* Set border color */
1409    data = snapdata[SNP_OFFSET +  2] & 0x07;
1410    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1411    EventList_Reset();
1412    border_set_last_color(data);
1413    border_force_redraw();
1414    logerror("Border color:%02X\n", data);
1415 
1416    spectrum_page_basicrom(machine);
1417 
1418    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1419}
1420 
1421/*******************************************************************
1422 *
1423 *      Load a .SNX file.
1424 *
1425 *      .SNX files were produced by an Atari ST emulator called Specci
1426 *      written by Christian Gandler
1427 *
1428 *      http://cd.textfiles.com/crawlycrypt1/apps/misc/zx_sp207/
1429 *
1430 *      The header is an extension of the .SNA format and the RAM dump
1431 *      is compressed with a simple run-length algorithm.
1432 *
1433 *      Offset  Size    Description (all registers stored with LSB first
1434 *                      except when noted with *BE*)
1435 *      ------- ------- -------------------------------------------------
1436 *      0       4       "XSNA" (signature)
1437 *      4       2       Header length *BE*
1438 *      6       1       I
1439 *      7       2       HL'
1440 *      9       2       DE'
1441 *      11      2       BC'
1442 *      13      2       AF'
1443 *      15      2       HL
1444 *      17      2       DE
1445 *      19      2       BC
1446 *      21      2       IY
1447 *      23      2       IX
1448 *      25      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
1449 *      26      1       R
1450 *      27      2       AF
1451 *      29      2       SP
1452 *      31      1       Interrupt Mode (0=IM0/1=IM1/2=IM2)
1453 *      32      1       Border color
1454 *      33      1       Specci settings: Interface 1 (0=no/1=yes)
1455 *      34      1       Specci settings: FLASH emulation (0=no/1=yes)
1456 *      35      1       Specci settings: Attributes emulation (0=no/1=yes)
1457 *      36      1       Specci settings: Bit 7 - Keyboard (0=QWERTY/1=QWERTZ)
1458 *                                       Bit 6 - ULA emulation (0=no/1=yes)
1459 *                                       Bit 0,1 - Joystick interface
1460 *                                            00 = Kempston
1461 *                                            01 = IF2 Left
1462 *                                            10 = IF2 Right
1463 *                                            11 = Cursor/AGF/Protek
1464 *      37      1       Specci settings: R register emulation (0=no/1=yes)
1465 *                                       Bit 7 - value of the EAR bit
1466 *      38      1       Specci settings: Interrupt frequency (0=50 Hz/1=100 Hz)
1467 *      39      1       Specci settings: RS232 redirection (0=RS232/1=Centronics)
1468 *      40      1       Specci settings: Sound emulation
1469 *                                       Bit 4,7 - Frequency (0..4) for mode 2
1470 *                                       Bit 0,3 - Mode (0=off/1=direct/2=interrupt)
1471 *      41      1       Specci settings: Border emulation (0=off/1=direct/2=interrupt)
1472 *      42      1       Specci settings: IM2 hardware vector (0..255)
1473 *      43      ?????   RAM dump
1474 *
1475 *      PC is stored on stack.
1476 *
1477 *      The RAM dump is divided into short or long datablocks:
1478 *      if the block is composed of the same byte, it is replaced by
1479 *      a compressed version. The format of the various blocks follows:
1480 *
1481 *      Uncompressed short block
1482 *      ------------------------
1483 *      Offset  Size    Description
1484 *      ------- ------- -------------------------------------------------
1485 *      0       1       Marker byte. Values between 0xe0 and 0xef.
1486 *                      Low nibble is (block length - 1).
1487 *      1       ?????   Data block
1488 *
1489 *      Compressed short block
1490 *      ----------------------
1491 *      Offset  Size    Description
1492 *      ------- ------- -------------------------------------------------
1493 *      0       1       Marker byte. Values between 0xf0 and 0xff.
1494 *                      Low nibble is (block length - 1).
1495 *      1       1       Filler byte. This byte is repeated for the whole
1496 *                      length of the block.
1497 *
1498 *      Uncompressed long block
1499 *      -----------------------
1500 *      Offset  Size    Description
1501 *      ------- ------- -------------------------------------------------
1502 *      0       2       Block length in big endian format.
1503 *                      Values between 0x0000 and 0xdfff.
1504 *      2       1       Uncompressed block flag: 0xff.
1505 *      3       ?????   Data block
1506 *
1507 *      Compressed long block
1508 *      ---------------------
1509 *      Offset  Size    Description
1510 *      ------- ------- -------------------------------------------------
1511 *      0       2       Block length in big endian format.
1512 *                      Values between 0x0000 and 0xdfff.
1513 *      2       1       Compressed block flag: 0x00.
1514 *      3       1       Filler byte. This byte is repeated for the whole
1515 *                      length of the block.
1516 *
1517 *******************************************************************/
1518static void spectrum_snx_decompress_block(running_machine *machine, UINT8 *source, UINT16 dest, UINT16 size)
1519{
1520    UINT8 counthi, countlo, compress, fill;
1521    UINT16 block, count, i, j, numbytes;
1522    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1523 
1524    i = SNX_HDR - 1;
1525    numbytes = 0;
1526 
1527    while (numbytes < size)
1528    {
1529        counthi = source[++i];
1530        if (counthi >= 0xe0)
1531        {
1532            count = (counthi & 0x0f) + 1;     // Short block
1533            if ((counthi & 0xf0) == 0xf0)
1534                compress = SNX_COMPRESSED;
1535            else
1536                compress = SNX_UNCOMPRESSED;
1537            logerror("Block:%05d  Type:Short  Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-1, count);
1538        }
1539        else
1540        {
1541            countlo = source[++i];
1542            count = (counthi << 8) | countlo; // Long block
1543            compress = source[++i];
1544            logerror("Block:%05d  Type:Long   Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-3, count);
1545        }
1546 
1547        if (compress == SNX_COMPRESSED)
1548        {
1549            fill = source[++i];
1550            logerror("Dest:%04X  Filler:%02X\n", SPECTRUM_BANK + numbytes, fill);
1551            for(j = 0; j < count; j++)
1552                memory_write_byte(space, SPECTRUM_BANK + numbytes + j, fill);
1553            numbytes += count;
1554        }
1555        else
1556        {
1557            logerror("Dest:%04X\n", SPECTRUM_BANK + numbytes);
1558            j = 0;
1559            while (j < count)
1560                memory_write_byte(space, SPECTRUM_BANK + numbytes + j++, source[++i]);
1561            numbytes += count;
1562        }
1563    }
1564}
1565 
1566void spectrum_setup_snx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1567{
1568    UINT8 intr;
1569    UINT16 data, addr;
1570    spectrum_state *state = (spectrum_state *)machine->driver_data;
1571    running_device *cpu = devtag_get_device(machine, "maincpu");
1572    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1573 
1574    data = (snapdata[SNX_OFFSET +  4] << 8) | snapdata[SNX_OFFSET +  5];
1575    if (data != 0x25)
1576      logerror("Corrupted header length: %02X instead of 0x25\n", data);
1577 
1578    data = (snapdata[SNX_OFFSET + 28] << 8) | snapdata[SNX_OFFSET + 27];
1579    LOAD_REG(cpu, Z80_AF, data);
1580 
1581    data = (snapdata[SNX_OFFSET + 20] << 8) | snapdata[SNX_OFFSET + 19];
1582    LOAD_REG(cpu, Z80_BC, data);
1583 
1584    data = (snapdata[SNX_OFFSET + 18] << 8) | snapdata[SNX_OFFSET + 17];
1585    LOAD_REG(cpu, Z80_DE, data);
1586 
1587    data = (snapdata[SNX_OFFSET + 16] << 8) | snapdata[SNX_OFFSET + 15];
1588    LOAD_REG(cpu, Z80_HL, data);
1589 
1590 
1591    data = (snapdata[SNX_OFFSET + 14] << 8) | snapdata[SNX_OFFSET + 13];
1592    LOAD_REG(cpu, Z80_AF2, data);
1593 
1594    data = (snapdata[SNX_OFFSET + 12] << 8) | snapdata[SNX_OFFSET + 11];
1595    LOAD_REG(cpu, Z80_BC2, data);
1596 
1597    data = (snapdata[SNX_OFFSET + 10] << 8) | snapdata[SNX_OFFSET +  9];
1598    LOAD_REG(cpu, Z80_DE2, data);
1599 
1600    data = (snapdata[SNX_OFFSET +  8] << 8) | snapdata[SNX_OFFSET +  7];
1601    LOAD_REG(cpu, Z80_HL2, data);
1602 
1603 
1604    data = (snapdata[SNX_OFFSET + 24] << 8) | snapdata[SNX_OFFSET + 23];
1605    LOAD_REG(cpu, Z80_IX, data);
1606 
1607    data = (snapdata[SNX_OFFSET + 22] << 8) | snapdata[SNX_OFFSET + 21];
1608    LOAD_REG(cpu, Z80_IY, data);
1609 
1610 
1611    data = snapdata[SNX_OFFSET + 26];
1612    LOAD_REG(cpu, Z80_R, data);
1613 
1614    data = snapdata[SNX_OFFSET +  6];
1615    LOAD_REG(cpu, Z80_I, data);
1616 
1617 
1618    data = (snapdata[SNX_OFFSET + 30] << 8) | snapdata[SNX_OFFSET + 29];
1619    LOAD_REG(cpu, Z80_SP, data);
1620 
1621 
1622    data = snapdata[SNX_OFFSET + 31] & 0x03;
1623    if (data == 3)
1624        data = 2;
1625    LOAD_REG(cpu, Z80_IM, data);
1626 
1627    data = snapdata[SNX_OFFSET + 25];
1628    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
1629    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
1630 
1631    intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
1632    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1633    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1634 
1635    /* Memory dump */
1636    logerror("Uncompressing %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1637    spectrum_snx_decompress_block(machine, snapdata, BASE_RAM, 3*SPECTRUM_BANK);
1638 
1639    /* get pc from stack */
1640    addr = cpu_get_reg(cpu, Z80_SP);
1641 
1642    if (addr < BASE_RAM)
1643      logerror("Corrupted SP points to ROM at:%04X", addr);
1644    else
1645      logerror("Fetching PC from the stack at SP:%04X\n", addr);
1646 
1647    LOAD_REG(cpu, Z80_PC, (memory_read_byte(space, addr + 1) << 8) | memory_read_byte(space, addr + 0));
1648 
1649#if 0
1650    memory_write_byte(space, addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
1651    memory_write_byte(space, addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
1652#endif
1653 
1654    addr += 2;
1655    logerror("Fixed the stack at SP:%04X\n", addr);
1656    cpu_set_reg(cpu, Z80_SP, addr);
1657 
1658    /* Set border color */
1659    data = snapdata[SNX_OFFSET + 32] & 0x07;
1660    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1661    EventList_Reset();
1662    border_set_last_color(data);
1663    border_force_redraw();
1664    logerror("Border color:%02X\n", data);
1665 
1666    spectrum_page_basicrom(machine);
1667 
1668/* TODO: Enable/disable IF1 as per snapdata[SNX_OFFSET + 33] */
1669 
1670/* TODO: Enable/disable joysticks as per snapdata[SNX_OFFSET + 36] */
1671 
1672    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1673}
1674 
1675static void spectrum_z80_decompress_block(running_machine *machine,UINT8 *pSource, int Dest, int size)
1676{
1677    UINT8 ch;
1678    int i;
1679    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1680 
1681    do
1682    {
1683        /* get byte */
1684        ch = pSource[0];
1685 
1686        /* either start 0f 0x0ed, 0x0ed, xx yy or
1687         * single 0x0ed */
1688        if (ch == (UINT8) 0x0ed)
1689        {
1690            if (pSource[1] == (UINT8) 0x0ed)
1691            {
1692 
1693                /* 0x0ed, 0x0ed, xx yy */
1694                /* repetition */
1695 
1696                int count;
1697                int data;
1698 
1699                count = (pSource[2] & 0x0ff);
1700 
1701                if (count == 0)
1702                    return;
1703 
1704                data = (pSource[3] & 0x0ff);
1705 
1706                pSource += 4;
1707 
1708                if (count > size)
1709                    count = size;
1710 
1711                size -= count;
1712 
1713                for (i = 0; i < count; i++)
1714                {
1715                    memory_write_byte(space,Dest, data);
1716                    Dest++;
1717                }
1718            }
1719            else
1720            {
1721                /* single 0x0ed */
1722                memory_write_byte(space,Dest, ch);
1723                Dest++;
1724                pSource++;
1725                size--;
1726 
1727            }
1728        }
1729        else
1730        {
1731            /* not 0x0ed */
1732            memory_write_byte(space,Dest, ch);
1733            Dest++;
1734            pSource++;
1735            size--;
1736        }
1737 
1738    }
1739    while (size > 0);
1740}
1741 
1742static SPECTRUM_Z80_SNAPSHOT_TYPE spectrum_identify_z80 (UINT8 *snapdata, UINT32 snapsize)
1743{
1744    UINT8 lo, hi, data;
1745 
1746    if (snapsize < 30)
1747        return SPECTRUM_Z80_SNAPSHOT_INVALID;   /* Invalid file */
1748 
1749    lo = snapdata[6] & 0x0ff;
1750    hi = snapdata[7] & 0x0ff;
1751    if (hi || lo)
1752        return SPECTRUM_Z80_SNAPSHOT_48K_OLD;   /* V1.45 - 48K only */
1753 
1754    lo = snapdata[30] & 0x0ff;
1755    hi = snapdata[31] & 0x0ff;
1756    data = snapdata[34] & 0x0ff;            /* Hardware mode */
1757 
1758    if ((hi == 0) && (lo == 23))
1759    {                       /* V2.01 */                         /* V2.01 format */
1760        switch (data)
1761        {
1762            case 0:
1763            case 1: return SPECTRUM_Z80_SNAPSHOT_48K;
1764            case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
1765            case 3:
1766            case 4: return SPECTRUM_Z80_SNAPSHOT_128K;
1767            case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
1768        }
1769    }
1770 
1771    if ((hi == 0) && (lo == 54))
1772    {                       /* V3.0x */                         /* V2.01 format */
1773        switch (data)
1774        {
1775            case 0:
1776            case 1:
1777            case 3: return SPECTRUM_Z80_SNAPSHOT_48K;
1778            case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
1779            case 4:
1780            case 5:
1781            case 6: return SPECTRUM_Z80_SNAPSHOT_128K;
1782            case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
1783        }
1784    }
1785 
1786    return SPECTRUM_Z80_SNAPSHOT_INVALID;
1787}
1788 
1789/* now supports 48k & 128k .Z80 files */
1790void spectrum_setup_z80(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1791{
1792    spectrum_state *state = (spectrum_state *)machine->driver_data;
1793    int i;
1794    UINT8 lo, hi, data;
1795    SPECTRUM_Z80_SNAPSHOT_TYPE z80_type;
1796    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1797 
1798    z80_type = spectrum_identify_z80(snapdata, snapsize);
1799 
1800    switch (z80_type)
1801    {
1802        case SPECTRUM_Z80_SNAPSHOT_INVALID:
1803                logerror("Invalid .Z80 file\n");
1804                return;
1805        case SPECTRUM_Z80_SNAPSHOT_48K_OLD:
1806        case SPECTRUM_Z80_SNAPSHOT_48K:
1807                logerror("48K .Z80 file\n");
1808                if (!strcmp(machine->gamedrv->name,"ts2068"))
1809                    logerror("48K .Z80 file in TS2068\n");
1810                break;
1811        case SPECTRUM_Z80_SNAPSHOT_128K:
1812                logerror("128K .Z80 file\n");
1813                if (state->port_7ffd_data == -1)
1814                {
1815                    logerror("Not a 48K .Z80 file\n");
1816                    return;
1817                }
1818                if (!strcmp(machine->gamedrv->name,"ts2068"))
1819                {
1820                    logerror("Not a TS2068 .Z80 file\n");
1821                    return;
1822                }
1823                break;
1824        case SPECTRUM_Z80_SNAPSHOT_TS2068:
1825                logerror("TS2068 .Z80 file\n");
1826                if (strcmp(machine->gamedrv->name,"ts2068"))
1827                    logerror("Not a TS2068 machine\n");
1828                break;
1829        case SPECTRUM_Z80_SNAPSHOT_SAMRAM:
1830                logerror("Hardware not supported - .Z80 file\n");
1831                return;
1832    }
1833 
1834    /* AF */
1835    hi = snapdata[0] & 0x0ff;
1836    lo = snapdata[1] & 0x0ff;
1837    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_AF, (hi << 8) | lo);
1838    /* BC */
1839    lo = snapdata[2] & 0x0ff;
1840    hi = snapdata[3] & 0x0ff;
1841    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_BC, (hi << 8) | lo);
1842    /* HL */
1843    lo = snapdata[4] & 0x0ff;
1844    hi = snapdata[5] & 0x0ff;
1845    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_HL, (hi << 8) | lo);
1846 
1847    /* SP */
1848    lo = snapdata[8] & 0x0ff;
1849    hi = snapdata[9] & 0x0ff;
1850    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_SP, (hi << 8) | lo);
1851 
1852    /* I */
1853    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_I, (snapdata[10] & 0x0ff));
1854 
1855    /* R */
1856    data = (snapdata[11] & 0x07f) | ((snapdata[12] & 0x01) << 7);
1857    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_R, data);
1858 
1859    /* Set border color */
1860    state->port_fe_data = (state->port_fe_data & 0xf8) | ((snapdata[12] & 0x0e) >> 1);
1861    EventList_Reset();
1862    border_set_last_color((snapdata[12] & 0x0e) >> 1);
1863    border_force_redraw();
1864 
1865    lo = snapdata[13] & 0x0ff;
1866    hi = snapdata[14] & 0x0ff;
1867    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_DE, (hi << 8) | lo);
1868 
1869    lo = snapdata[15] & 0x0ff;
1870    hi = snapdata[16] & 0x0ff;
1871    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_BC2, (hi << 8) | lo);
1872 
1873    lo = snapdata[17] & 0x0ff;
1874    hi = snapdata[18] & 0x0ff;
1875    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_DE2, (hi << 8) | lo);
1876 
1877    lo = snapdata[19] & 0x0ff;
1878    hi = snapdata[20] & 0x0ff;
1879    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_HL2, (hi << 8) | lo);
1880 
1881    hi = snapdata[21] & 0x0ff;
1882    lo = snapdata[22] & 0x0ff;
1883    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_AF2, (hi << 8) | lo);
1884 
1885    lo = snapdata[23] & 0x0ff;
1886    hi = snapdata[24] & 0x0ff;
1887    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IY, (hi << 8) | lo);
1888 
1889    lo = snapdata[25] & 0x0ff;
1890    hi = snapdata[26] & 0x0ff;
1891    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IX, (hi << 8) | lo);
1892 
1893    /* Interrupt Flip/Flop */
1894    if (snapdata[27] == 0)
1895    {
1896        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF1, 0);
1897        /* cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IRQ_STATE, 0); */
1898    }
1899    else
1900    {
1901        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF1, 1);
1902        /* cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IRQ_STATE, 1); */
1903    }
1904 
1905    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, data);
1906//  cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, data);
1907    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1908 
1909    /* IFF2 */
1910    if (snapdata[28] != 0)
1911    {
1912        data = 1;
1913    }
1914    else
1915    {
1916        data = 0;
1917    }
1918    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF2, data);
1919 
1920    /* Interrupt Mode */
1921    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IM, (snapdata[29] & 0x03));
1922 
1923    if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K_OLD)
1924    {
1925        lo = snapdata[6] & 0x0ff;
1926        hi = snapdata[7] & 0x0ff;
1927        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_PC, (hi << 8) | lo);
1928 
1929        spectrum_page_basicrom(machine);
1930 
1931        if ((snapdata[12] & 0x020) == 0)
1932        {
1933            logerror("Not compressed\n");   /* not compressed */
1934            for (i = 0; i < 49152; i++)
1935                memory_write_byte(space,i + 16384, snapdata[30 + i]);
1936        }
1937        else
1938        {
1939            logerror("Compressed\n");   /* compressed */
1940            spectrum_z80_decompress_block(machine, snapdata + 30, 16384, 49152);
1941        }
1942    }
1943    else
1944    {
1945        UINT8 *pSource;
1946        int header_size;
1947 
1948        header_size = 30 + 2 + ((snapdata[30] & 0x0ff) | ((snapdata[31] & 0x0ff) << 8));
1949 
1950        lo = snapdata[32] & 0x0ff;
1951        hi = snapdata[33] & 0x0ff;
1952        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_PC, (hi << 8) | lo);
1953 
1954        if ((z80_type == SPECTRUM_Z80_SNAPSHOT_128K) || ((z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068) && !strcmp(machine->gamedrv->name,"ts2068")))
1955        {
1956            running_device *ay8912 = devtag_get_device(machine, "ay8912");
1957 
1958            /* Only set up sound registers for 128K machine or TS2068! */
1959            for (i = 0; i < 16; i++)
1960            {
1961                ay8910_address_w(ay8912, 0, i);
1962                ay8910_data_w(ay8912, 0, snapdata[39 + i]);
1963            }
1964            ay8910_address_w(ay8912, 0, snapdata[38]);
1965        }
1966 
1967        pSource = snapdata + header_size;
1968 
1969        if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K)
1970            /* Ensure 48K Basic ROM is used */
1971            spectrum_page_basicrom(machine);
1972 
1973        do
1974        {
1975            unsigned short length;
1976            UINT8 page;
1977            int Dest = 0;
1978 
1979            length = (pSource[0] & 0x0ff) | ((pSource[1] & 0x0ff) << 8);
1980            page = pSource[2];
1981 
1982            if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K || z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068)
1983            {
1984                switch (page)
1985                {
1986                    case 4: Dest = 0x08000; break;
1987                    case 5: Dest = 0x0c000; break;
1988                    case 8: Dest = 0x04000; break;
1989                    default: Dest = 0; break;
1990                }
1991            }
1992            else
1993            {
1994                /* 3 = bank 0, 4 = bank 1 ... 10 = bank 7 */
1995                if ((page >= 3) && (page <= 10))
1996                {
1997                    /* Page the appropriate bank into 0xc000 - 0xfff */
1998                    state->port_7ffd_data = page - 3;
1999                    spectrum_update_paging(machine);
2000                    Dest = 0x0c000;
2001                }
2002                else
2003                    /* Other values correspond to ROM pages */
2004                    Dest = 0x0;
2005            }
2006 
2007            if (Dest != 0)
2008            {
2009                if (length == 0x0ffff)
2010                {
2011                    /* block is uncompressed */
2012                    logerror("Not compressed\n");
2013 
2014                    /* not compressed */
2015                    for (i = 0; i < 16384; i++)
2016                        memory_write_byte(space,i + Dest, pSource[i]);
2017                }
2018                else
2019                {
2020                    logerror("Compressed\n");
2021 
2022                    /* block is compressed */
2023                    spectrum_z80_decompress_block(machine,&pSource[3], Dest, 16384);
2024                }
2025            }
2026 
2027            /* go to next block */
2028            pSource += (3 + length);
2029        }
2030        while ((pSource - snapdata) < snapsize);
2031 
2032        if ((state->port_7ffd_data != -1) && (z80_type != SPECTRUM_Z80_SNAPSHOT_48K))
2033        {
2034            /* Set up paging */
2035            state->port_7ffd_data = (snapdata[35] & 0x0ff);
2036            spectrum_update_paging(machine);
2037        }
2038        if ((z80_type == SPECTRUM_Z80_SNAPSHOT_48K) && !strcmp(machine->gamedrv->name,"ts2068"))
2039        {
2040            state->port_f4_data = 0x03;
2041            state->port_ff_data = 0x00;
2042            ts2068_update_memory(machine);
2043        }
2044        if (z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068 && !strcmp(machine->gamedrv->name,"ts2068"))
2045        {
2046            state->port_f4_data = snapdata[35];
2047            state->port_ff_data = snapdata[36];
2048            ts2068_update_memory(machine);
2049        }
2050    }
2051}
2052 
2053QUICKLOAD_LOAD(spectrum)
2054{
2055    const address_space *space = cputag_get_address_space(image->machine, "maincpu", ADDRESS_SPACE_PROGRAM);
2056    int i;
2057    int quick_addr = BASE_RAM;
2058    int quick_length;
2059    UINT8 *quick_data;
2060    int read_;
2061 
2062    quick_length = image_length(image);
2063    quick_data = (UINT8 *)malloc(quick_length);
2064    if (!quick_data)
2065        return INIT_FAIL;
2066 
2067    read_ = image_fread(image, quick_data, quick_length);
2068    if (read_ != quick_length)
2069        return INIT_FAIL;
2070 
2071    for (i = 0; i < quick_length; i++)
2072        memory_write_byte(space,i + quick_addr, quick_data[i]);
2073 
2074    logerror("quick loading at %.4x size:%.4x\n", quick_addr, quick_length);
2075    return INIT_PASS;
1/***************************************************************************
2
3  mess/formats/spec_snqk.c
4
5  TODO:
6
7    - Implement the following snapshot formats
8      .89C (Tezxas)
9      .FRZ (CBSpeccy)
10      .PLUSD (DISCiPLE/+D)
11      .SLT (Various emulators)
12      .SZX (Spectaculator)
13      .ZLS (ZX-Live!)
14      .ZX (ZXSpectr)
15      .ZX82 (Speculator '97)
16      .ZXS (ZX32)
17
18    - Implement the following quickload formats
19      .$? (HoBeta)
20      .__? (SpecEm)
21      .BAS (BASin)
22      .H/.B (Roman ZX)
23      .RAW (Various emulators)
24      .SCR variants
25
26    - Cleanup of the .Z80 format
27
28***************************************************************************/
29
30#include "emu.h"
31#include "cpu/z80/z80.h"
32#include "includes/spectrum.h"
33#include "eventlst.h"
34#include "video/border.h"
35#include "sound/ay8910.h"
36#include "formats/spec_snqk.h"
37#include "utils.h"
38
39
40/*******************************************************************
41 *
42 *      Update the memory and paging of the spectrum being emulated
43 *
44 *      if port_7ffd_data is -1 then machine is 48K - no paging
45 *      if port_1ffd_data is -1 then machine is 128K
46 *      if neither port is -1 then machine is +2a/+3
47 *
48 *      Note: the 128K .SNA and .Z80 file formats do not store the
49 *      port 1FFD setting so it is necessary to calculate the appropriate
50 *      value for the ROM paging.
51 *
52 *******************************************************************/
53static void spectrum_update_paging(running_machine *machine)
54{
55    spectrum_state *state = (spectrum_state *)machine->driver_data;
56    if (state->port_7ffd_data == -1)
57        return;
58    if (state->port_1ffd_data == -1)
59        spectrum_128_update_memory(machine);
60
61    else
62    {
63        if (BIT(state->port_7ffd_data, 4))
64            /* Page in Spec 48K basic ROM */
65            state->port_1ffd_data = 0x04;
66        else
67            state->port_1ffd_data = 0x00;
68        spectrum_plus3_update_memory(machine);
69    }
70}
71
72/* Page in the 48K Basic ROM. Used when running 48K snapshots on a 128K machine. */
73static void spectrum_page_basicrom(running_machine *machine)
74{
75    spectrum_state *state = (spectrum_state *)machine->driver_data;
76    if (state->port_7ffd_data == -1)
77        return;
78    state->port_7ffd_data |= 0x10;
79    spectrum_update_paging(machine);
80}
81
82
83SNAPSHOT_LOAD(spectrum)
84{
85    UINT8 *snapshot_data = NULL;
86
87    snapshot_data = (UINT8*)malloc(snapshot_size);
88    if (!snapshot_data)
89        goto error;
90
91    image_fread(image, snapshot_data, snapshot_size);
92
93    if (!mame_stricmp(file_type, "sna"))
94    {
95        if ((snapshot_size != SNA48_SIZE) && (snapshot_size != SNA128_SIZE_1) && (snapshot_size != SNA128_SIZE_2))
96        {
97            logerror("Invalid .SNA file size.\n");
98            goto error;
99        }
100        spectrum_setup_sna(image->machine, snapshot_data, snapshot_size);
101    }
102    else if (!mame_stricmp(file_type, "sp"))
103    {
104        if ((snapshot_data[0] != 'S' && snapshot_data[1] != 'P') && (snapshot_size != SP_NEW_SIZE_16K || snapshot_size != SP_NEW_SIZE_48K))
105        {
106            if (snapshot_size != SP_OLD_SIZE)
107            {
108                logerror("Invalid .SP file size.\n");
109                goto error;
110            }
111        }
112        spectrum_setup_sp(image->machine, snapshot_data, snapshot_size);
113    }
114    else if (!mame_stricmp(file_type, "ach"))
115    {
116        if (snapshot_size != ACH_SIZE)
117        {
118            logerror("Invalid .ACH file size.\n");
119            goto error;
120        }
121        spectrum_setup_ach(image->machine, snapshot_data, snapshot_size);
122    }
123    else if (!mame_stricmp(file_type, "prg"))
124    {
125        if (snapshot_size != PRG_SIZE)
126        {
127            logerror("Invalid .PRG file size.\n");
128            goto error;
129        }
130        spectrum_setup_prg(image->machine, snapshot_data, snapshot_size);
131    }
132    else if (!mame_stricmp(file_type, "sem"))
133    {
134        if (snapshot_data[0] != 0x05 && snapshot_data[1] != 'S' && \
135            snapshot_data[2] != 'P' && snapshot_data[3] != 'E' && \
136            snapshot_data[4] != 'C' && snapshot_data[5] != '1')
137        {
138            if (snapshot_size != SEM_SIZE)
139            {
140                logerror("Invalid .SEM file size.\n");
141                goto error;
142            }
143        }
144        spectrum_setup_sem(image->machine, snapshot_data, snapshot_size);
145    }
146    else if (!mame_stricmp(file_type, "sit"))
147    {
148        if (snapshot_size != SIT_SIZE)
149        {
150            logerror("Invalid .SIT file size.\n");
151            goto error;
152        }
153        spectrum_setup_sit(image->machine, snapshot_data, snapshot_size);
154    }
155    else if (!mame_stricmp(file_type, "zx"))
156    {
157        if (snapshot_size != ZX_SIZE)
158        {
159            logerror("Invalid .ZX file size.\n");
160            goto error;
161        }
162        spectrum_setup_zx(image->machine, snapshot_data, snapshot_size);
163    }
164    else if (!mame_stricmp(file_type, "snp"))
165    {
166        if (snapshot_size != SNP_SIZE)
167        {
168            logerror("Invalid .SNP file size.\n");
169            goto error;
170        }
171        spectrum_setup_snp(image->machine, snapshot_data, snapshot_size);
172    }
173    else if (!mame_stricmp(file_type, "snx"))
174    {
175        if (snapshot_data[0] != 'X' && snapshot_data[1] != 'S' && \
176            snapshot_data[2] != 'N' && snapshot_data[3] != 'A')
177        {
178          logerror("Invalid .SEM file size.\n");
179          goto error;
180        }
181        spectrum_setup_snx(image->machine, snapshot_data, snapshot_size);
182    }
183    else
184    {
185        spectrum_setup_z80(image->machine, snapshot_data, snapshot_size);
186    }
187
188    free(snapshot_data);
189
190    return INIT_PASS;
191
192error:
193    if (snapshot_data)
194        free(snapshot_data);
195    return INIT_FAIL;
196}
197
198/*******************************************************************
199 *
200 *      Load a .SP file.
201 *
202 *      There are two kinds of .SP files: "old" and "new".
203 *
204 *      The old version is always 49184 bytes long and is created
205 *      by a leaked copy of the VGASpec emulator.
206 *
207 *      Subsequently Pedro Gimeno (the author of VGASpec) renamed it
208 *      to "Spectrum" (but it's colloquially known as the "Spanish
209 *      Spectrum emulator") and extended the header to break backward
210 *      compatibility: the new format supports both 16K and 48K
211 *      images and it's 16422 or 49190 bytes long.
212 *
213 *      http://www.formauri.es/personal/pgimeno/spec/spec.html
214 *
215 *      The formats are defined as follows:
216 *
217 *      Offset              Size    Description (all registers stored with LSB first)
218 *      ------------------- ------- -------------------------------------------------
219 *      VGASPEC SPECTRUM
220 *      ------------------- ------- -------------------------------------------------
221 *      N/A     0           2       "SP" (signature)
222 *      N/A     2           2       Program length
223 *      N/A     4           2       Program location
224 *      0       6           2       BC
225 *      2       8           2       DE
226 *      4       10          2       HL
227 *      6       12          2       AF
228 *      8       14          2       IX
229 *      10      16          2       IY
230 *      12      18          2       BC'
231 *      14      20          2       DE'
232 *      16      22          2       HL'
233 *      18      24          2       AF'
234 *      20      26          1       R
235 *      21      27          1       I
236 *      22      28          2       SP
237 *      24      30          2       PC
238 *      26      32          2       0 (reserved for future use)
239 *      28      34          1       Border color
240 *      29      35          1       0 (reserved for future use)
241 *      30      36          2       Status word
242 *      32      38          16384/  RAM dump
243 *                          49152
244 *
245 *  Status word:
246 *  Bit     Description
247 *  ------- ---------------------------------------------------
248 *  15-8    Reserved for future use
249 *  7-6     Reserved for internal use (0)
250 *  5       Status of the flash attribute: 0=normal/1=inverse
251 *  4       Interrupt pending for execution
252 *  3       If 1, IM 0; if 0, bit 1 determines interrupt mode
253 *              (Spectrum v0.99e had this behaviour reversed,
254 *              and this bit was not used in versions previous
255 *              to v 0.99e)
256 *  2       IFF2: 0=DI/1=EI
257 *  1       Interrupt mode (if bit 3 reset): 0=IM1/1=IM2
258 *  0       IFF1: 0=DI/1=EI
259 *
260 *******************************************************************/
261void spectrum_setup_sp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
262{
263    int i, SP_OFFSET;
264    UINT8 intr;
265    UINT16 start, size, data, status;
266    spectrum_state *state = (spectrum_state *)machine->driver_data;
267    running_device *cpu = devtag_get_device(machine, "maincpu");
268    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
269
270    if (snapsize == SP_NEW_SIZE_16K || snapsize == SP_NEW_SIZE_48K)
271    {
272        SP_OFFSET = 0;
273        start = (snapdata[SP_OFFSET +  5] << 8) | snapdata[SP_OFFSET +  4];
274        size = (snapdata[SP_OFFSET +  3] << 8) | snapdata[SP_OFFSET +  2];
275    }
276    else
277    {
278        SP_OFFSET = SP_OLD_HDR - SP_NEW_HDR;
279        start = BASE_RAM;
280        size = 3*SPECTRUM_BANK;
281    }
282
283    data = (snapdata[SP_OFFSET + 13] << 8) | snapdata[SP_OFFSET + 12];
284    LOAD_REG(cpu, Z80_AF, data);
285
286    data = (snapdata[SP_OFFSET +  7] << 8) | snapdata[SP_OFFSET +  6];
287    LOAD_REG(cpu, Z80_BC, data);
288
289    data = (snapdata[SP_OFFSET +  9] << 8) | snapdata[SP_OFFSET +  8];
290    LOAD_REG(cpu, Z80_DE, data);
291
292    data = (snapdata[SP_OFFSET + 11] << 8) | snapdata[SP_OFFSET + 10];
293    LOAD_REG(cpu, Z80_HL, data);
294
295
296    data = (snapdata[SP_OFFSET + 25] << 8) | snapdata[SP_OFFSET + 24];
297    LOAD_REG(cpu, Z80_AF2, data);
298
299    data = (snapdata[SP_OFFSET + 19] << 8) | snapdata[SP_OFFSET + 18];
300    LOAD_REG(cpu, Z80_BC2, data);
301
302    data = (snapdata[SP_OFFSET + 21] << 8) | snapdata[SP_OFFSET + 20];
303    LOAD_REG(cpu, Z80_DE2, data);
304
305    data = (snapdata[SP_OFFSET + 23] << 8) | snapdata[SP_OFFSET + 22];
306    LOAD_REG(cpu, Z80_HL2, data);
307
308
309    data = (snapdata[SP_OFFSET + 15] << 8) | snapdata[SP_OFFSET + 14];
310    LOAD_REG(cpu, Z80_IX, data);
311
312    data = (snapdata[SP_OFFSET + 17] << 8) | snapdata[SP_OFFSET + 16];
313    LOAD_REG(cpu, Z80_IY, data);
314
315
316    data = snapdata[SP_OFFSET + 26];
317    LOAD_REG(cpu, Z80_R, data);
318
319    data = snapdata[SP_OFFSET + 27];
320    LOAD_REG(cpu, Z80_I, data);
321
322
323    data = (snapdata[SP_OFFSET + 29] << 8) | snapdata[SP_OFFSET + 28];
324    LOAD_REG(cpu, Z80_SP, data);
325
326    data = (snapdata[SP_OFFSET + 31] << 8) | snapdata[SP_OFFSET + 30];
327    LOAD_REG(cpu, Z80_PC, data);
328
329
330    status = (snapdata[SP_OFFSET + 37] << 8) | snapdata[SP_OFFSET + 36];
331
332    data = (BIT(status, 3) << 1) | BIT(status, 1);
333    switch(data)
334    {
335      case 0: // case 2: in version 0.99e of the emulator
336        LOAD_REG(cpu, Z80_IM, 1);
337        break;
338      case 1: // case 3: in version 0.99e of the emulator
339        LOAD_REG(cpu, Z80_IM, 2);
340        break;
341      case 2: // case 0: in version 0.99e of the emulator
342      case 3: // case 1: in version 0.99e of the emulator
343        LOAD_REG(cpu, Z80_IM, 0);
344    }
345
346    data = BIT(status, 0);
347    LOAD_REG(cpu, Z80_IFF1, data);
348
349    data = BIT(status, 2);
350    LOAD_REG(cpu, Z80_IFF2, data);
351
352    intr = BIT(status, 4) ? ASSERT_LINE : CLEAR_LINE;
353    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
354    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
355
356    data = BIT(status, 5);
357    /* TODO: Flash bit */
358
359    /* Memory dump */
360    logerror("Loading %04X bytes of RAM at %04X\n", size, start);
361    for (i = 0; i < size; i++)
362        memory_write_byte(space, start + i, snapdata[SP_OFFSET + SP_NEW_HDR + i]);
363
364    /* Set border color */
365    data = snapdata[SP_OFFSET + 34] & 0x07;
366    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
367    EventList_Reset();
368    border_set_last_color(data);
369    border_force_redraw();
370    logerror("Border color:%02X\n", data);
371
372    spectrum_page_basicrom(machine);
373
374    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
375}
376
377/*******************************************************************
378 *
379 *      Load a .SNA file.
380 *
381 *      This format was used by Arnt Gulbrandsen for its JPP
382 *      emulator, and it's based on the format developed for the Mirage
383 *      Microdriver, a Microdrive backup accessory.
384 *
385 *      http://www.worldofspectrum.org/infoseekid.cgi?id=1000266
386 *
387 *      The 48K image is always 49179 bytes long.
388 *
389 *      .SNA snapshots save the PC on the top of the stack, thus
390 *      deleting the original bytes: not a good feature for a snapshot...
391 *
392 *      It's been reported that snapshots saved at critical moments would
393 *      subsequently crash on restore due to the fact that the stack was full
394 *      or pointed to a ROM address. A workaround for this situation is to
395 *      store 0x0000 into the word that stored the PC.
396 *
397 *      For sake of fidelity, the code that clears the top of the stack has
398 *      been written but commented out.
399 *
400 *      Later, the format has been extended to store the whole Spectrum 128 status.
401 *
402 *      48K format as follows:
403 *
404 *      Offset  Size    Description (all registers stored with LSB first)
405 *      ------- ------- -------------------------------------------------
406 *      0       1       I
407 *      1       2       HL'
408 *      3       2       DE'
409 *      5       2       BC'
410 *      7       2       AF'
411 *      9       2       HL
412 *      11      2       DE
413 *      13      2       BC
414 *      15      2       IY
415 *      17      2       IX
416 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
417 *      20      1       R
418 *      21      2       AF
419 *      23      2       SP
420 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
421 *      26      1       Border color
422 *      27      49152   RAM dump
423 *
424 *      PC is stored on stack.
425 *
426 *      128K format as follows:
427 *
428 *      Offset  Size    Description (all registers stored with LSB first)
429 *      ------- ------- -------------------------------------------------
430 *      0       1       I
431 *      1       2       HL'
432 *      3       2       DE'
433 *      5       2       BC'
434 *      7       2       AF'
435 *      9       2       HL
436 *      11      2       DE
437 *      13      2       BC
438 *      15      2       IY
439 *      17      2       IX
440 *      19      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
441 *      20      1       R
442 *      21      2       AF
443 *      23      2       SP
444 *      25      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
445 *      26      1       Border color
446 *      27      16384   RAM bank 5 (0x4000-0x7fff)
447 *      16411   16384   RAM bank 2 (0x8000-0xbfff)
448 *      32795   16384   RAM bank n (0xc000-0xffff - currently paged bank)
449 *      49179   2       PC
450 *      49181   1       Port 7FFD data
451 *      49182   1       TR-DOS rom paged (0=no/1=yes)
452 *      49183   81920/  Remaining RAM banks in ascending order
453 *              98304
454 *
455 *      PC is NOT stored on stack.
456 *
457 *      The bank in 0xc000 is always included even if it is page 2 or 5
458 *      in which case it is included twice.
459 *
460 *******************************************************************/
461void spectrum_setup_sna(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
462{
463    int i, j, usedbanks[8];
464    long bank_offset;
465    UINT8 intr;
466    UINT16 data, addr;
467    spectrum_state *state = (spectrum_state *)machine->driver_data;
468    running_device *cpu = devtag_get_device(machine, "maincpu");
469    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
470
471    if ((snapsize != SNA48_SIZE) && (state->port_7ffd_data == -1))
472    {
473        logerror("Can't load 128K .SNA file into 48K machine\n");
474        return;
475    }
476
477
478    data = (snapdata[SNA48_OFFSET + 22] << 8) | snapdata[SNA48_OFFSET + 21];
479    LOAD_REG(cpu, Z80_AF, data);
480
481    data = (snapdata[SNA48_OFFSET + 14] << 8) | snapdata[SNA48_OFFSET + 13];
482    LOAD_REG(cpu, Z80_BC, data);
483
484    data = (snapdata[SNA48_OFFSET + 12] << 8) | snapdata[SNA48_OFFSET + 11];
485    LOAD_REG(cpu, Z80_DE, data);
486
487    data = (snapdata[SNA48_OFFSET + 10] << 8) | snapdata[SNA48_OFFSET +  9];
488    LOAD_REG(cpu, Z80_HL, data);
489
490
491    data = (snapdata[SNA48_OFFSET +  8] << 8) | snapdata[SNA48_OFFSET +  7];
492    LOAD_REG(cpu, Z80_AF2, data);
493
494    data = (snapdata[SNA48_OFFSET +  6] << 8) | snapdata[SNA48_OFFSET +  5];
495    LOAD_REG(cpu, Z80_BC2, data);
496
497    data = (snapdata[SNA48_OFFSET +  4] << 8) | snapdata[SNA48_OFFSET +  3];
498    LOAD_REG(cpu, Z80_DE2, data);
499
500    data = (snapdata[SNA48_OFFSET +  2] << 8) | snapdata[SNA48_OFFSET +  1];
501    LOAD_REG(cpu, Z80_HL2, data);
502
503
504    data = (snapdata[SNA48_OFFSET + 18] << 8) | snapdata[SNA48_OFFSET + 17];
505    LOAD_REG(cpu, Z80_IX, data);
506
507    data = (snapdata[SNA48_OFFSET + 16] << 8) | snapdata[SNA48_OFFSET + 15];
508    LOAD_REG(cpu, Z80_IY, data);
509
510
511    data = snapdata[SNA48_OFFSET + 20];
512    LOAD_REG(cpu, Z80_R, data);
513
514    data = snapdata[SNA48_OFFSET +  0];
515    LOAD_REG(cpu, Z80_I, data);
516
517
518    data = (snapdata[SNA48_OFFSET + 24] << 8) | snapdata[SNA48_OFFSET + 23];
519    LOAD_REG(cpu, Z80_SP, data);
520
521
522    data = snapdata[SNA48_OFFSET + 25] & 0x03;
523    if (data == 3)
524        data = 2;
525    LOAD_REG(cpu, Z80_IM, data);
526
527    data = snapdata[SNA48_OFFSET + 19];
528    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
529    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
530
531    intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
532    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
533    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
534
535    if (snapsize == SNA48_SIZE)
536        /* 48K Snapshot */
537        spectrum_page_basicrom(machine);
538    else
539    {
540        /* 128K Snapshot */
541        state->port_7ffd_data = snapdata[SNA128_OFFSET + 2];
542        if (snapdata[SNA128_OFFSET + 3])
543        {
544          /* TODO: page TR-DOS ROM when supported */
545        }
546        spectrum_update_paging(machine);
547    }
548
549    if (snapsize == SNA48_SIZE)
550    {
551        /* Memory dump */
552        logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
553        for (i = 0; i < 3*SPECTRUM_BANK; i++)
554          memory_write_byte(space, BASE_RAM + i, snapdata[SNA48_HDR + i]);
555
556        /* Get PC from stack */
557        addr = cpu_get_reg(cpu, Z80_SP);
558
559        if (addr < BASE_RAM)
560          logerror("Corrupted SP points to ROM at:%04X", addr);
561        else
562          logerror("Fetching PC from the stack at SP:%04X\n", addr);
563
564        data = (memory_read_byte(space, addr + 1) << 8) | memory_read_byte(space, addr + 0);
565        LOAD_REG(cpu, Z80_PC, data);
566
567#if 0
568        memory_write_byte(space, addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
569        memory_write_byte(space, addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
570#endif
571
572        addr += 2;
573        logerror("Fixing SP:%04X\n", addr);
574        cpu_set_reg(cpu, Z80_SP, addr);
575
576        /* Set border color */
577        data = snapdata[SNA48_OFFSET + 26] & 0x07;
578        state->port_fe_data = (state->port_fe_data & 0xf8) | data;
579        EventList_Reset();
580        border_set_last_color(data);
581        border_force_redraw();
582        logerror("Border color:%02X\n", data);
583
584        logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
585    }
586    else
587    {
588        /* Set up other RAM banks */
589        bank_offset = SNA48_SIZE + SNA128_OFFSET;
590        for (i = 0; i < 8; i++)
591            usedbanks[i] = 0;
592
593        usedbanks[5] = 1;                               /* 0x4000-0x7fff */
594        usedbanks[2] = 1;                               /* 0x8000-0xbfff */
595        usedbanks[state->port_7ffd_data & 0x07] = 1;    /* Banked memory */
596
597        logerror("Loading %05X bytes of RAM at %04X\n", 8*SPECTRUM_BANK, BASE_RAM);
598        logerror("Loading bank 5 from offset:0001B\n");
599        logerror("Loading bank 2 from offset:0401B\n");
600        logerror("Loading bank %d from offset:0801B\n", snapdata[SNA128_OFFSET + 2] & 0x07);
601        for (i = 0; i < 3*SPECTRUM_BANK; i++)
602          memory_write_byte(space, BASE_RAM + i, snapdata[SNA48_HDR + i]);
603
604        for (i = 0; i < 8; i++)
605        {
606            if (!usedbanks[i])
607            {
608                logerror("Loading bank %d from offset:%05lX\n", i, bank_offset);
609                state->port_7ffd_data &= 0xf8;
610                state->port_7ffd_data += i;
611                spectrum_update_paging(machine);
612                for (j = 0; j < SPECTRUM_BANK; j++)
613                    memory_write_byte(space,j + 3*SPECTRUM_BANK, snapdata[bank_offset + j]);
614                bank_offset += SPECTRUM_BANK;
615            }
616        }
617
618        /* program counter */
619        data = (snapdata[SNA128_OFFSET + 1] << 8) | snapdata[SNA128_OFFSET + 0];
620        LOAD_REG(cpu, Z80_PC, data);
621
622        /* Set border color */
623        data = snapdata[SNA48_OFFSET + 26] & 0x07;
624        state->port_fe_data = (state->port_fe_data & 0xf8) | data;
625        EventList_Reset();
626        border_set_last_color(data);
627        border_force_redraw();
628        logerror("Border color:%02X\n", data);
629        data = state->port_7ffd_data & 0x07;
630
631        /* Reset paging */
632        state->port_7ffd_data = snapdata[SNA128_OFFSET + 2];
633        spectrum_update_paging(machine);
634
635        logerror("Snapshot loaded.\nExecution resuming at Bank:%d %s\n", data, cpu_get_reg_string(cpu, Z80_PC));
636    }
637}
638
639/*******************************************************************
640 *
641 *      Load a .ACH file.
642 *
643 *      .ACH files were produced by a commercial Archimedes
644 *      (hence the name) emulator called !Speccy.
645 *
646 *      The files are always 65792 bytes long and store both
647 *      a copy of the ROM and the whole 48K RAM.
648 *
649 *
650 *      Offset  Size    Description (all registers stored with LSB first
651 *                      except when noted with *BE*)
652 *      ------- ------- -------------------------------------------------
653 *      0       4       A
654 *      4       4       F
655 *      8       4       B
656 *      12      4       C
657 *      16      4       D
658 *      20      4       E
659 *      24      4       H
660 *      28      4       L
661 *      32      4       PC
662 *      36      4       0 (reserved for future use)
663 *      40      4       SP
664 *      44      104     0 (reserved for future use)
665 *      148     4       R
666 *      152     4       0 (reserved for future use)
667 *      156     4       Border color
668 *      160     4       0 (reserved for future use)
669 *      164     2       Interrupt mode (0=IM0/1=IM1/2=IM2)
670 *      166     24      0 (reserved for future use)
671 *      190     1       I
672 *      191     1       IFF1/2: (0=DI/1=EI)
673 *      192     44      0 (reserved for future use)
674 *      236     4       AF' *BE*
675 *      240     4       BC' *BE*
676 *      244     2       DE' *BE*
677 *      246     2       HL' *BE*
678 *      248     4       IX
679 *      252     4       IY
680 *      256     16384   ROM dump
681 *      16640   49152   RAM dump
682 *
683 *******************************************************************/
684void spectrum_setup_ach(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
685{
686    int i;
687    UINT8 intr;
688    UINT16 data;
689    spectrum_state *state = (spectrum_state *)machine->driver_data;
690    running_device *cpu = devtag_get_device(machine, "maincpu");
691    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
692
693    data = (snapdata[ACH_OFFSET +   0] << 8) | snapdata[ACH_OFFSET +   4];
694    LOAD_REG(cpu, Z80_AF, data);
695
696    data = (snapdata[ACH_OFFSET +   8] << 8) | snapdata[ACH_OFFSET +  12];
697    LOAD_REG(cpu, Z80_BC, data);
698
699    data = (snapdata[ACH_OFFSET +  16] << 8) | snapdata[ACH_OFFSET +  20];
700    LOAD_REG(cpu, Z80_DE, data);
701
702    data = (snapdata[ACH_OFFSET +  24] << 8) | snapdata[ACH_OFFSET +  28];
703    LOAD_REG(cpu, Z80_HL, data);
704
705
706    data = (snapdata[ACH_OFFSET + 236] << 8) | snapdata[ACH_OFFSET + 237];
707    LOAD_REG(cpu, Z80_AF2, data);
708
709    data = (snapdata[ACH_OFFSET + 240] << 8) | snapdata[ACH_OFFSET + 241];
710    LOAD_REG(cpu, Z80_BC2, data);
711
712    data = (snapdata[ACH_OFFSET + 244] << 8) | snapdata[ACH_OFFSET + 245];
713    LOAD_REG(cpu, Z80_DE2, data);
714
715    data = (snapdata[ACH_OFFSET + 246] << 8) | snapdata[ACH_OFFSET + 247];
716    LOAD_REG(cpu, Z80_HL2, data);
717
718
719    data = (snapdata[ACH_OFFSET + 249] << 8) | snapdata[ACH_OFFSET + 248];
720    LOAD_REG(cpu, Z80_IX, data);
721
722    data = (snapdata[ACH_OFFSET + 253] << 8) | snapdata[ACH_OFFSET + 252];
723    LOAD_REG(cpu, Z80_IY, data);
724
725    data = snapdata[ACH_OFFSET + 148];
726    LOAD_REG(cpu, Z80_R, data);
727
728    data = snapdata[ACH_OFFSET + 190];
729    LOAD_REG(cpu, Z80_I, data);
730
731
732    data = (snapdata[ACH_OFFSET +  41] << 8) | snapdata[ACH_OFFSET +  40];
733    LOAD_REG(cpu, Z80_SP, data);
734
735    data = (snapdata[ACH_OFFSET +  33] << 8) | snapdata[ACH_OFFSET +  32];
736    LOAD_REG(cpu, Z80_PC, data);
737
738    data = snapdata[ACH_OFFSET + 164] & 0x03;
739    if (data == 3)
740        data = 0;
741    LOAD_REG(cpu, Z80_IM, data);
742
743    data = snapdata[ACH_OFFSET + 191] ? 1 : 0;
744    LOAD_REG(cpu, Z80_IFF1, data);
745    LOAD_REG(cpu, Z80_IFF2, data);
746
747    intr = snapdata[ACH_OFFSET + 191] ? CLEAR_LINE : ASSERT_LINE;
748    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
749    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
750
751    logerror("Skipping the 16K ROM dump at offset:%04X\n", ACH_OFFSET + 256);
752
753    /* Memory dump */
754    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
755    for (i = 0; i < 3*SPECTRUM_BANK; i++)
756        memory_write_byte(space, BASE_RAM + i, snapdata[ACH_HDR + SPECTRUM_BANK + i]);
757
758    /* Set border color */
759    data = snapdata[ACH_OFFSET + 156] & 0x07;
760    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
761    EventList_Reset();
762    border_set_last_color(data);
763    border_force_redraw();
764    logerror("Border color:%02X\n", data);
765
766    spectrum_page_basicrom(machine);
767
768    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
769}
770
771/*******************************************************************
772 *
773 *      Load a .PRG file.
774 *
775 *      .PRG files were produced by an irish emulator called SpecEm
776 *      written by Kevin Phair (thanks for the help, Kevin!)
777 *
778 *      http://code.google.com/p/specem/
779 *
780 *      The files are always 49408 bytes long and use some stack space
781 *      to store registers.
782 *
783 *      The header actually mimics the file descriptor format used by
784 *      the DISCiPLE/+D interface.
785 *
786 *      Offset  Size    Description (all registers stored with LSB first
787 *                      except when noted with *BE*)
788 *      ------- ------- -------------------------------------------------
789 *      0       1       File type (always 0x05, corresponding to "48K Snapshot"
790 *      1       10      Program name (padded with 0x20)
791 *      11      2       Number of sectors occupied by the file *BE*
792 *      13      1       Track number of the first sector of the file
793 *      14      1       Sector number of the first sector of the file
794 *      15      195     Sector allocation bitmap
795 *      210     10      0 (reserved for future use)
796 *      220     2       IY
797 *      222     2       IX
798 *      224     2       DE'
799 *      226     2       BC'
800 *      228     2       HL'
801 *      230     2       AF'
802 *      232     2       DE
803 *      234     2       BC
804 *      236     2       HL
805 *      238     1       IFF1/2: (bit 2, 0=DI/1=EI)
806 *      239     1       I
807 *      240     2       SP
808 *      242     14      0 (reserved for future use)
809 *      256     49152   RAM dump
810 *
811 *      R,AF and PC are stored on the stack. Due to a bug in the
812 *      BIOS, the snapshots created with the +D have the AF' register
813 *      replaced with the AF register.
814 *
815 *      It's unknown (but likely possible) that the snapshots could
816 *      suffer from the same "top of the stack" bug as well as .SNA images.
817 *
818 *******************************************************************/
819void spectrum_setup_prg(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
820{
821    int i;
822    UINT8 intr;
823    UINT16 addr, data;
824    spectrum_state *state = (spectrum_state *)machine->driver_data;
825    running_device *cpu = devtag_get_device(machine, "maincpu");
826    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
827
828    data = snapdata[PRG_OFFSET +   0];
829    if (data != 0x05)
830      logerror("Wrong DISCiPLE/+D file type: %02X instead of 05\n", data);
831
832    data = (snapdata[PRG_OFFSET + 235] << 8) | snapdata[PRG_OFFSET + 234];
833    LOAD_REG(cpu, Z80_BC, data);
834
835    data = (snapdata[PRG_OFFSET + 233] << 8) | snapdata[PRG_OFFSET + 232];
836    LOAD_REG(cpu, Z80_DE, data);
837
838    data = (snapdata[PRG_OFFSET + 237] << 8) | snapdata[PRG_OFFSET + 236];
839    LOAD_REG(cpu, Z80_HL, data);
840
841
842    data = (snapdata[PRG_OFFSET + 231] << 8) | snapdata[PRG_OFFSET + 230];
843    LOAD_REG(cpu, Z80_AF2, data);
844
845    data = (snapdata[PRG_OFFSET + 227] << 8) | snapdata[PRG_OFFSET + 226];
846    LOAD_REG(cpu, Z80_BC2, data);
847
848    data = (snapdata[PRG_OFFSET + 225] << 8) | snapdata[PRG_OFFSET + 224];
849    LOAD_REG(cpu, Z80_DE2, data);
850
851    data = (snapdata[PRG_OFFSET + 229] << 8) | snapdata[PRG_OFFSET + 228];
852    LOAD_REG(cpu, Z80_HL2, data);
853
854
855    data = (snapdata[PRG_OFFSET + 223] << 8) | snapdata[PRG_OFFSET + 222];
856    LOAD_REG(cpu, Z80_IX, data);
857
858    data = (snapdata[PRG_OFFSET + 221] << 8) | snapdata[PRG_OFFSET + 220];
859    LOAD_REG(cpu, Z80_IY, data);
860
861    data = snapdata[PRG_OFFSET + 239];
862    LOAD_REG(cpu, Z80_I, data);
863
864    LOAD_REG(cpu, Z80_IM, (data == 0x00 || data == 0x3f) ? 1 : 2);
865
866    data = snapdata[PRG_OFFSET + 238];
867    LOAD_REG(cpu, Z80_IFF1, BIT(data, 2));
868    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
869
870    intr = BIT(data, 2) ? CLEAR_LINE : ASSERT_LINE;
871    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
872    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
873
874    /* Memory dump */
875    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
876    for (i = 0; i < 3*SPECTRUM_BANK; i++)
877        memory_write_byte(space, BASE_RAM + i, snapdata[PRG_HDR + i]);
878
879    addr = (snapdata[PRG_OFFSET + 241] << 8) | snapdata[PRG_OFFSET + 240];
880    if (addr < BASE_RAM)
881      logerror("Corrupted SP points to ROM at:%04X", addr);
882    else
883      logerror("Fetching registers R, AF and PC from the stack at SP:%04X\n", addr);
884
885    data = memory_read_byte(space, addr + 0); // Junk created when pushing R on the stack via LD A,R/PUSH AF
886
887    data = memory_read_byte(space, addr + 1);
888    LOAD_REG(cpu, Z80_R, data);
889
890    data = (memory_read_byte(space, addr + 3) << 8) | memory_read_byte(space, addr + 2);
891    LOAD_REG(cpu, Z80_AF, data);
892
893    data = (memory_read_byte(space, addr + 5) << 8) | memory_read_byte(space, addr + 4);
894    LOAD_REG(cpu, Z80_PC, data);
895
896    addr += 6;
897    logerror("Fixing SP:%04X\n", addr);
898    cpu_set_reg(cpu, Z80_SP, addr);
899
900    /* Set border color */
901    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
902    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
903    EventList_Reset();
904    border_set_last_color(data);
905    border_force_redraw();
906    logerror("Border color:%02X\n", data);
907
908    spectrum_page_basicrom(machine);
909
910    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
911}
912
913/*******************************************************************
914 *
915 *      Load a .SEM file.
916 *
917 *      .SEM files were produced by a german emulator called SpecEmu
918 *      written by Bernd Waschke.
919 *
920 *      The files are usually 49192 bytes long unless optional POKE
921 *      blocks are stored at the end of the file.
922 *
923 *      Offset  Size    Description (all registers stored with LSB first)
924 *      ------- ------- -------------------------------------------------
925 *      0       1       0x05 (signature length)
926 *      1       5       "SPEC1" (signature)
927 *      6       49152   RAM dump
928 *      49158   2       AF
929 *      49160   2       BC
930 *      49162   2       DE
931 *      49164   2       HL
932 *      49166   2       AF'
933 *      49168   2       BC'
934 *      49170   2       DE'
935 *      49172   2       HL'
936 *      49174   2       PC
937 *      49176   2       SP
938 *      49178   2       IX
939 *      49180   2       IY
940 *      49182   2       I
941 *      49184   2       R
942 *      49186   2       IFF1 (0=DI/1=EI)
943 *      49188   2       IFF2 (0=DI/1=EI)
944 *      49190   2       Interrupt mode (0=IM0/1=IM1/2=IM2)
945 *
946 *      Following these data, there are optional POKE blocks
947 *
948 *******************************************************************/
949void spectrum_setup_sem(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
950{
951    int i;
952    UINT8 intr;
953    UINT16 data;
954    spectrum_state *state = (spectrum_state *)machine->driver_data;
955    running_device *cpu = devtag_get_device(machine, "maincpu");
956    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
957
958    data = (snapdata[SEM_OFFSET +  1] << 8) | snapdata[SEM_OFFSET +  0];
959    LOAD_REG(cpu, Z80_AF, data);
960
961    data = (snapdata[SEM_OFFSET +  3] << 8) | snapdata[SEM_OFFSET +  2];
962    LOAD_REG(cpu, Z80_BC, data);
963
964    data = (snapdata[SEM_OFFSET +  5] << 8) | snapdata[SEM_OFFSET +  4];
965    LOAD_REG(cpu, Z80_DE, data);
966
967    data = (snapdata[SEM_OFFSET +  7] << 8) | snapdata[SEM_OFFSET +  6];
968    LOAD_REG(cpu, Z80_HL, data);
969
970
971    data = (snapdata[SEM_OFFSET +  9] << 8) | snapdata[SEM_OFFSET +  8];
972    LOAD_REG(cpu, Z80_AF2, data);
973
974    data = (snapdata[SEM_OFFSET + 11] << 8) | snapdata[SEM_OFFSET + 10];
975    LOAD_REG(cpu, Z80_BC2, data);
976
977    data = (snapdata[SEM_OFFSET + 13] << 8) | snapdata[SEM_OFFSET + 12];
978    LOAD_REG(cpu, Z80_DE2, data);
979
980    data = (snapdata[SEM_OFFSET + 15] << 8) | snapdata[SEM_OFFSET + 14];
981    LOAD_REG(cpu, Z80_HL2, data);
982
983
984    data = (snapdata[SEM_OFFSET + 21] << 8) | snapdata[SEM_OFFSET + 20];
985    LOAD_REG(cpu, Z80_IX, data);
986
987    data = (snapdata[SEM_OFFSET + 23] << 8) | snapdata[SEM_OFFSET + 22];
988    LOAD_REG(cpu, Z80_IY, data);
989
990
991    data = snapdata[SEM_OFFSET + 26];
992    LOAD_REG(cpu, Z80_R, data);
993
994    data = snapdata[SEM_OFFSET + 24];
995    LOAD_REG(cpu, Z80_I, data);
996
997
998    data = (snapdata[SEM_OFFSET + 19] << 8) | snapdata[SEM_OFFSET + 18];
999    LOAD_REG(cpu, Z80_SP, data);
1000
1001    data = (snapdata[SEM_OFFSET + 17] << 8) | snapdata[SEM_OFFSET + 16];
1002    LOAD_REG(cpu, Z80_PC, data);
1003
1004    data = snapdata[SEM_OFFSET + 32];
1005    LOAD_REG(cpu, Z80_IM, data);
1006
1007    data = snapdata[SEM_OFFSET + 28];
1008    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
1009    data = snapdata[SEM_OFFSET + 30];
1010    LOAD_REG(cpu, Z80_IFF2, BIT(data, 0));
1011
1012    intr = BIT(snapdata[SEM_OFFSET + 30], 0) ? CLEAR_LINE : ASSERT_LINE;
1013    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1014    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1015
1016    /* Memory dump */
1017    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1018    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1019        memory_write_byte(space, BASE_RAM + i, snapdata[SEM_SIGNATURE + i]);
1020
1021    /* Set border color */
1022    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
1023    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1024    EventList_Reset();
1025    border_set_last_color(data);
1026    border_force_redraw();
1027    logerror("Border color:%02X\n", data);
1028
1029    spectrum_page_basicrom(machine);
1030
1031    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1032
1033/* TODO: Decode the optional POKE bank at the end of the image */
1034
1035}
1036
1037/*******************************************************************
1038 *
1039 *      Load a .SIT file.
1040 *
1041 *      .SIT files were produced by a spanish emulator called Sinclair
1042 *      written by Pedro M. R. Salas.
1043 *
1044 *      http://www.ugr.es/~pedrom/sinclair.htm
1045 *
1046 *      The files are always 65564 bytes long and store both ROM and RAM.
1047 *
1048 *      Offset  Size    Description (all registers stored with LSB first)
1049 *      ------- ------- -------------------------------------------------
1050 *      0       2       BC
1051 *      2       2       DE
1052 *      4       2       HL
1053 *      6       2       AF
1054 *      8       2       IX
1055 *      10      2       IY
1056 *      12      2       SP
1057 *      14      2       PC
1058 *      16      1       R
1059 *      17      1       I
1060 *      18      2       BC'
1061 *      20      2       DE'
1062 *      22      2       HL'
1063 *      24      2       AF'
1064 *      26      1       Interrupt mode (0=IM0/1=IM1/2=IM2)
1065 *      27      1       Border color
1066 *      28      16384   ROM dump
1067 *      16412   49152   RAM dump
1068 *
1069 *******************************************************************/
1070void spectrum_setup_sit(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1071{
1072    int i;
1073    UINT8 intr;
1074    UINT16 data;
1075    spectrum_state *state = (spectrum_state *)machine->driver_data;
1076    running_device *cpu = devtag_get_device(machine, "maincpu");
1077    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1078
1079    data = (snapdata[SIT_OFFSET +  7] << 8) | snapdata[SIT_OFFSET +  6];
1080    LOAD_REG(cpu, Z80_AF, data);
1081
1082    data = (snapdata[SIT_OFFSET +  1] << 8) | snapdata[SIT_OFFSET +  0];
1083    LOAD_REG(cpu, Z80_BC, data);
1084
1085    data = (snapdata[SIT_OFFSET +  3] << 8) | snapdata[SIT_OFFSET +  2];
1086    LOAD_REG(cpu, Z80_DE, data);
1087
1088    data = (snapdata[SIT_OFFSET +  5] << 8) | snapdata[SIT_OFFSET +  4];
1089    LOAD_REG(cpu, Z80_HL, data);
1090
1091
1092    data = (snapdata[SIT_OFFSET + 25] << 8) | snapdata[SIT_OFFSET + 24];
1093    LOAD_REG(cpu, Z80_AF2, data);
1094
1095    data = (snapdata[SIT_OFFSET + 19] << 8) | snapdata[SIT_OFFSET + 18];
1096    LOAD_REG(cpu, Z80_BC2, data);
1097
1098    data = (snapdata[SIT_OFFSET + 21] << 8) | snapdata[SIT_OFFSET + 20];
1099    LOAD_REG(cpu, Z80_DE2, data);
1100
1101    data = (snapdata[SIT_OFFSET + 23] << 8) | snapdata[SIT_OFFSET + 22];
1102    LOAD_REG(cpu, Z80_HL2, data);
1103
1104
1105    data = (snapdata[SIT_OFFSET +  9] << 8) | snapdata[SIT_OFFSET +  8];
1106    LOAD_REG(cpu, Z80_IX, data);
1107
1108    data = (snapdata[SIT_OFFSET + 11] << 8) | snapdata[SIT_OFFSET + 10];
1109    LOAD_REG(cpu, Z80_IY, data);
1110
1111
1112    data = snapdata[SIT_OFFSET + 16];
1113    LOAD_REG(cpu, Z80_R, data);
1114
1115    data = snapdata[SIT_OFFSET + 17];
1116    LOAD_REG(cpu, Z80_I, data);
1117
1118
1119    data = (snapdata[SIT_OFFSET + 13] << 8) | snapdata[SIT_OFFSET + 12];
1120    LOAD_REG(cpu, Z80_SP, data);
1121
1122    data = (snapdata[SIT_OFFSET + 15] << 8) | snapdata[SIT_OFFSET + 14];
1123    LOAD_REG(cpu, Z80_PC, data);
1124
1125    data = snapdata[SIT_OFFSET + 26];
1126    LOAD_REG(cpu, Z80_IM, data);
1127
1128    data = 0x01; // .SIT snapshots don't specify whether interrupts are enabled or not, so I assume they are.
1129    LOAD_REG(cpu, Z80_IFF1, data);
1130    LOAD_REG(cpu, Z80_IFF2, data);
1131
1132    intr = data ? CLEAR_LINE : ASSERT_LINE;
1133    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1134    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1135
1136    /* Memory dump */
1137    logerror("Skipping the 16K ROM dump at offset:%04X\n", SIT_OFFSET + 28);
1138    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1139    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1140        memory_write_byte(space, BASE_RAM + i, snapdata[SIT_HDR + SPECTRUM_BANK + i]);
1141
1142    /* Set border color */
1143    data = snapdata[SIT_OFFSET + 27] & 0x07;
1144    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1145    EventList_Reset();
1146    border_set_last_color(data);
1147    border_force_redraw();
1148    logerror("Border color:%02X\n", data);
1149
1150    spectrum_page_basicrom(machine);
1151
1152    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1153}
1154
1155/*******************************************************************
1156 *
1157 *      Load a .ZX file.
1158 *
1159 *      .ZX files were produced by the Amiga emulator called KGB
1160 *
1161 *      The files are usually 49486 bytes and store at the beginning
1162 *      of the file the last 132 bytes of the ROM.
1163 *
1164 *      Offset  Size    Description (all registers stored with MSB first)
1165 *      ------- ------- -------------------------------------------------
1166 *      0       132     Last 132 bytes of ROM dump
1167 *      132     49152   RAM dump
1168 *      49284   132     0 (reserved for future use)
1169 *      49416   10      Various KGB settings
1170 *      49426   1       IFF1/2: (0=DI/1=EI)
1171 *      49427   2       Reserved (must be 0x0003)
1172 *      49429   1       KGB ColorMode (0=BW/1=Color)
1173 *      49430   4       0 (reserved for future use)
1174 *      49434   2       BC
1175 *      49436   2       BC'
1176 *      49438   2       DE
1177 *      49440   2       DE'
1178 *      49442   2       HL
1179 *      49444   2       HL'
1180 *      49446   2       IX
1181 *      49448   2       IY
1182 *      49450   1       I
1183 *      49451   1       R
1184 *      49452   3       0 (reserved for future use)
1185 *      49455   1       A'
1186 *      49456   1       0 (reserved for future use)
1187 *      49457   1       A
1188 *      49458   1       0 (reserved for future use)
1189 *      49459   1       F'
1190 *      49460   1       0 (reserved for future use)
1191 *      49461   1       F
1192 *      49462   2       0 (reserved for future use)
1193 *      49464   2       PC
1194 *      49466   2       0 (reserved for future use)
1195 *      49468   2       SP
1196 *      49470   2       KGB Soundmode (0=Simple/1=Pitch/2=RomOnly)
1197 *      49472   2       KGB HaltMode (0=NoHalt/1=Halt)
1198 *      49474   2       Interrupt mode (-1=IM0/0=IM1/1=IM2)
1199 *      49476   10      0 (reserved for future use)
1200 *
1201 *******************************************************************/
1202void spectrum_setup_zx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1203{
1204    int i;
1205    UINT8 intr;
1206    UINT16 data, mode;
1207    spectrum_state *state = (spectrum_state *)machine->driver_data;
1208    running_device *cpu = devtag_get_device(machine, "maincpu");
1209    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1210
1211    logerror("Skipping last 132 bytes of the 16K ROM dump at offset:0000\n");
1212
1213    data = (snapdata[ZX_OFFSET + 173] << 8) | snapdata[ZX_OFFSET + 177];
1214    LOAD_REG(cpu, Z80_AF, data);
1215
1216    data = (snapdata[ZX_OFFSET + 150] << 8) | snapdata[ZX_OFFSET + 151];
1217    LOAD_REG(cpu, Z80_BC, data);
1218
1219    data = (snapdata[ZX_OFFSET + 154] << 8) | snapdata[ZX_OFFSET + 155];
1220    LOAD_REG(cpu, Z80_DE, data);
1221
1222    data = (snapdata[ZX_OFFSET + 158] << 8) | snapdata[ZX_OFFSET + 159];
1223    LOAD_REG(cpu, Z80_HL, data);
1224
1225
1226    data = (snapdata[ZX_OFFSET + 171] << 8) | snapdata[ZX_OFFSET + 175];
1227    LOAD_REG(cpu, Z80_AF2, data);
1228
1229    data = (snapdata[ZX_OFFSET + 152] << 8) | snapdata[ZX_OFFSET + 153];
1230    LOAD_REG(cpu, Z80_BC2, data);
1231
1232    data = (snapdata[ZX_OFFSET + 156] << 8) | snapdata[ZX_OFFSET + 157];
1233    LOAD_REG(cpu, Z80_DE2, data);
1234
1235    data = (snapdata[ZX_OFFSET + 160] << 8) | snapdata[ZX_OFFSET + 161];
1236    LOAD_REG(cpu, Z80_HL2, data);
1237
1238
1239    data = (snapdata[ZX_OFFSET + 162] << 8) | snapdata[ZX_OFFSET + 163];
1240    LOAD_REG(cpu, Z80_IX, data);
1241
1242    data = (snapdata[ZX_OFFSET + 164] << 8) | snapdata[ZX_OFFSET + 165];
1243    LOAD_REG(cpu, Z80_IY, data);
1244
1245
1246    data = snapdata[ZX_OFFSET + 167];
1247    LOAD_REG(cpu, Z80_R, data);
1248
1249    data = snapdata[ZX_OFFSET + 166];
1250    LOAD_REG(cpu, Z80_I, data);
1251
1252
1253    data = (snapdata[ZX_OFFSET + 184] << 8) | snapdata[ZX_OFFSET + 185];
1254    LOAD_REG(cpu, Z80_SP, data);
1255
1256    data = (snapdata[ZX_OFFSET + 180] << 8) | snapdata[ZX_OFFSET + 181];
1257    LOAD_REG(cpu, Z80_PC, data);
1258
1259    mode = (snapdata[ZX_OFFSET + 190] << 8) | snapdata[ZX_OFFSET + 191];
1260    switch (mode)
1261    {
1262      case 0xffff:
1263        LOAD_REG(cpu, Z80_IM, 0);
1264        break;
1265      case 0x00:
1266        LOAD_REG(cpu, Z80_IM, 1);
1267        break;
1268      case 0x01:
1269        LOAD_REG(cpu, Z80_IM, 2);
1270        break;
1271      default:
1272        logerror("Invalid IM:%04X\n", mode);
1273    }
1274
1275    data = BIT(snapdata[ZX_OFFSET + 142], 0);
1276    LOAD_REG(cpu, Z80_IFF1, data);
1277    LOAD_REG(cpu, Z80_IFF2, data);
1278
1279    intr = BIT(snapdata[ZX_OFFSET + 142], 0) ? CLEAR_LINE : ASSERT_LINE;
1280    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1281    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1282
1283    /* Memory dump */
1284    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1285    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1286        memory_write_byte(space, BASE_RAM + i, snapdata[132 + i]);
1287
1288    /* Set border color */
1289    data = (memory_read_byte(space, 0x5c48) >> 3) & 0x07; // Get the current border color from BORDCR system variable.
1290    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1291    EventList_Reset();
1292    border_set_last_color(data);
1293    border_force_redraw();
1294    logerror("Border color:%02X\n", data);
1295
1296    spectrum_page_basicrom(machine);
1297
1298    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1299}
1300
1301/*******************************************************************
1302 *
1303 *      Load a .SNP file.
1304 *
1305 *      .SNP files were produced by a polish emulator called Nuclear ZX
1306 *      written by Radovan Garabik and Lubomir Salanci.
1307 *
1308 *      http://korpus.juls.savba.sk/~garabik/old/zx.html
1309 *
1310 *      The files are always 49183 bytes long.
1311 *
1312 *      Offset  Size    Description (all registers stored with LSB first)
1313 *      ------- ------- -------------------------------------------------
1314 *      0       49152   RAM dump
1315 *      49152   2       AF
1316 *      49154   1       Border color
1317 *      49155   1       0 (reserved for future use)
1318 *      49156   2       BC
1319 *      49158   2       DE
1320 *      49160   2       HL
1321 *      49162   2       PC
1322 *      49164   2       SP
1323 *      49166   2       IX
1324 *      49168   2       IY
1325 *      49170   1       0 (reserved for IFF2 but not implemented)
1326 *      49171   1       IFF1 (0=DI/other=EI)
1327 *      49172   1       Interrupt mode (0=IM0/1=IM1/2=IM2)
1328 *      49173   1       R
1329 *      49174   1       I
1330 *      49175   2       AF'
1331 *      49177   2       BC'
1332 *      49179   2       DE'
1333 *      49181   2       HL'
1334 *
1335 *******************************************************************/
1336void spectrum_setup_snp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1337{
1338    int i;
1339    UINT8 intr;
1340    UINT16 data;
1341    spectrum_state *state = (spectrum_state *)machine->driver_data;
1342    running_device *cpu = devtag_get_device(machine, "maincpu");
1343    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1344
1345    data = (snapdata[SNP_OFFSET +  1] << 8) | snapdata[SNP_OFFSET +  0];
1346    LOAD_REG(cpu, Z80_AF, data);
1347
1348    data = (snapdata[SNP_OFFSET +  5] << 8) | snapdata[SNP_OFFSET +  4];
1349    LOAD_REG(cpu, Z80_BC, data);
1350
1351    data = (snapdata[SNP_OFFSET +  7] << 8) | snapdata[SNP_OFFSET +  6];
1352    LOAD_REG(cpu, Z80_DE, data);
1353
1354    data = (snapdata[SNP_OFFSET +  9] << 8) | snapdata[SNP_OFFSET +  8];
1355    LOAD_REG(cpu, Z80_HL, data);
1356
1357
1358    data = (snapdata[SNP_OFFSET + 24] << 8) | snapdata[SNP_OFFSET + 23];
1359    LOAD_REG(cpu, Z80_AF2, data);
1360
1361    data = (snapdata[SNP_OFFSET + 26] << 8) | snapdata[SNP_OFFSET + 25];
1362    LOAD_REG(cpu, Z80_BC2, data);
1363
1364    data = (snapdata[SNP_OFFSET + 28] << 8) | snapdata[SNP_OFFSET + 27];
1365    LOAD_REG(cpu, Z80_DE2, data);
1366
1367    data = (snapdata[SNP_OFFSET + 30] << 8) | snapdata[SNP_OFFSET + 29];
1368    LOAD_REG(cpu, Z80_HL2, data);
1369
1370
1371    data = (snapdata[SNP_OFFSET + 15] << 8) | snapdata[SNP_OFFSET + 14];
1372    LOAD_REG(cpu, Z80_IX, data);
1373
1374    data = (snapdata[SNP_OFFSET + 17] << 8) | snapdata[SNP_OFFSET + 16];
1375    LOAD_REG(cpu, Z80_IY, data);
1376
1377
1378    data = snapdata[SNP_OFFSET + 21];
1379    LOAD_REG(cpu, Z80_R, data);
1380
1381    data = snapdata[SNP_OFFSET + 22];
1382    LOAD_REG(cpu, Z80_I, data);
1383
1384
1385    data = (snapdata[SNP_OFFSET + 13] << 8) | snapdata[SNP_OFFSET + 12];
1386    LOAD_REG(cpu, Z80_SP, data);
1387
1388    data = (snapdata[SNP_OFFSET + 11] << 8) | snapdata[SNP_OFFSET + 10];
1389    LOAD_REG(cpu, Z80_PC, data);
1390
1391
1392    data = snapdata[SNP_OFFSET + 91] & 0x03;
1393    LOAD_REG(cpu, Z80_IM, data);
1394
1395    data = BIT(snapdata[SNP_OFFSET + 19], 0);
1396    LOAD_REG(cpu, Z80_IFF1, data);
1397    LOAD_REG(cpu, Z80_IFF2, data);
1398
1399    intr = BIT(snapdata[SNP_OFFSET + 19], 0) ? CLEAR_LINE : ASSERT_LINE;
1400    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1401    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1402
1403    /* Memory dump */
1404    logerror("Loading %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1405    for (i = 0; i < 3*SPECTRUM_BANK; i++)
1406        memory_write_byte(space, BASE_RAM + i, snapdata[i]);
1407
1408    /* Set border color */
1409    data = snapdata[SNP_OFFSET +  2] & 0x07;
1410    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1411    EventList_Reset();
1412    border_set_last_color(data);
1413    border_force_redraw();
1414    logerror("Border color:%02X\n", data);
1415
1416    spectrum_page_basicrom(machine);
1417
1418    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1419}
1420
1421/*******************************************************************
1422 *
1423 *      Load a .SNX file.
1424 *
1425 *      .SNX files were produced by an Atari ST emulator called Specci
1426 *      written by Christian Gandler
1427 *
1428 *      http://cd.textfiles.com/crawlycrypt1/apps/misc/zx_sp207/
1429 *
1430 *      The header is an extension of the .SNA format and the RAM dump
1431 *      is compressed with a simple run-length algorithm.
1432 *
1433 *      Offset  Size    Description (all registers stored with LSB first
1434 *                      except when noted with *BE*)
1435 *      ------- ------- -------------------------------------------------
1436 *      0       4       "XSNA" (signature)
1437 *      4       2       Header length *BE*
1438 *      6       1       I
1439 *      7       2       HL'
1440 *      9       2       DE'
1441 *      11      2       BC'
1442 *      13      2       AF'
1443 *      15      2       HL
1444 *      17      2       DE
1445 *      19      2       BC
1446 *      21      2       IY
1447 *      23      2       IX
1448 *      25      1       IFF1/2: bit 0 contains IFF1, bit 2 contains IFF2 (0=DI/1=EI)
1449 *      26      1       R
1450 *      27      2       AF
1451 *      29      2       SP
1452 *      31      1       Interrupt Mode (0=IM0/1=IM1/2=IM2)
1453 *      32      1       Border color
1454 *      33      1       Specci settings: Interface 1 (0=no/1=yes)
1455 *      34      1       Specci settings: FLASH emulation (0=no/1=yes)
1456 *      35      1       Specci settings: Attributes emulation (0=no/1=yes)
1457 *      36      1       Specci settings: Bit 7 - Keyboard (0=QWERTY/1=QWERTZ)
1458 *                                       Bit 6 - ULA emulation (0=no/1=yes)
1459 *                                       Bit 0,1 - Joystick interface
1460 *                                            00 = Kempston
1461 *                                            01 = IF2 Left
1462 *                                            10 = IF2 Right
1463 *                                            11 = Cursor/AGF/Protek
1464 *      37      1       Specci settings: R register emulation (0=no/1=yes)
1465 *                                       Bit 7 - value of the EAR bit
1466 *      38      1       Specci settings: Interrupt frequency (0=50 Hz/1=100 Hz)
1467 *      39      1       Specci settings: RS232 redirection (0=RS232/1=Centronics)
1468 *      40      1       Specci settings: Sound emulation
1469 *                                       Bit 4,7 - Frequency (0..4) for mode 2
1470 *                                       Bit 0,3 - Mode (0=off/1=direct/2=interrupt)
1471 *      41      1       Specci settings: Border emulation (0=off/1=direct/2=interrupt)
1472 *      42      1       Specci settings: IM2 hardware vector (0..255)
1473 *      43      ?????   RAM dump
1474 *
1475 *      PC is stored on stack.
1476 *
1477 *      The RAM dump is divided into short or long datablocks:
1478 *      if the block is composed of the same byte, it is replaced by
1479 *      a compressed version. The format of the various blocks follows:
1480 *
1481 *      Uncompressed short block
1482 *      ------------------------
1483 *      Offset  Size    Description
1484 *      ------- ------- -------------------------------------------------
1485 *      0       1       Marker byte. Values between 0xe0 and 0xef.
1486 *                      Low nibble is (block length - 1).
1487 *      1       ?????   Data block
1488 *
1489 *      Compressed short block
1490 *      ----------------------
1491 *      Offset  Size    Description
1492 *      ------- ------- -------------------------------------------------
1493 *      0       1       Marker byte. Values between 0xf0 and 0xff.
1494 *                      Low nibble is (block length - 1).
1495 *      1       1       Filler byte. This byte is repeated for the whole
1496 *                      length of the block.
1497 *
1498 *      Uncompressed long block
1499 *      -----------------------
1500 *      Offset  Size    Description
1501 *      ------- ------- -------------------------------------------------
1502 *      0       2       Block length in big endian format.
1503 *                      Values between 0x0000 and 0xdfff.
1504 *      2       1       Uncompressed block flag: 0xff.
1505 *      3       ?????   Data block
1506 *
1507 *      Compressed long block
1508 *      ---------------------
1509 *      Offset  Size    Description
1510 *      ------- ------- -------------------------------------------------
1511 *      0       2       Block length in big endian format.
1512 *                      Values between 0x0000 and 0xdfff.
1513 *      2       1       Compressed block flag: 0x00.
1514 *      3       1       Filler byte. This byte is repeated for the whole
1515 *                      length of the block.
1516 *
1517 *******************************************************************/
1518static void spectrum_snx_decompress_block(running_machine *machine, UINT8 *source, UINT16 dest, UINT16 size)
1519{
1520    UINT8 counthi, countlo, compress, fill;
1521    UINT16 block, count, i, j, numbytes;
1522    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1523
1524    i = SNX_HDR - 1;
1525    numbytes = 0;
1526
1527    while (numbytes < size)
1528    {
1529        counthi = source[++i];
1530        if (counthi >= 0xe0)
1531        {
1532            count = (counthi & 0x0f) + 1;     // Short block
1533            if ((counthi & 0xf0) == 0xf0)
1534                compress = SNX_COMPRESSED;
1535            else
1536                compress = SNX_UNCOMPRESSED;
1537            logerror("Block:%05d  Type:Short  Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-1, count);
1538        }
1539        else
1540        {
1541            countlo = source[++i];
1542            count = (counthi << 8) | countlo; // Long block
1543            compress = source[++i];
1544            logerror("Block:%05d  Type:Long   Compr:%s  Offset:%04X  Len:%04X  ", block++, compress == SNX_COMPRESSED ? "Y" : "N", i-3, count);
1545        }
1546
1547        if (compress == SNX_COMPRESSED)
1548        {
1549            fill = source[++i];
1550            logerror("Dest:%04X  Filler:%02X\n", SPECTRUM_BANK + numbytes, fill);
1551            for(j = 0; j < count; j++)
1552                memory_write_byte(space, SPECTRUM_BANK + numbytes + j, fill);
1553            numbytes += count;
1554        }
1555        else
1556        {
1557            logerror("Dest:%04X\n", SPECTRUM_BANK + numbytes);
1558            j = 0;
1559            while (j < count)
1560                memory_write_byte(space, SPECTRUM_BANK + numbytes + j++, source[++i]);
1561            numbytes += count;
1562        }
1563    }
1564}
1565
1566void spectrum_setup_snx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1567{
1568    UINT8 intr;
1569    UINT16 data, addr;
1570    spectrum_state *state = (spectrum_state *)machine->driver_data;
1571    running_device *cpu = devtag_get_device(machine, "maincpu");
1572    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1573
1574    data = (snapdata[SNX_OFFSET +  4] << 8) | snapdata[SNX_OFFSET +  5];
1575    if (data != 0x25)
1576      logerror("Corrupted header length: %02X instead of 0x25\n", data);
1577
1578    data = (snapdata[SNX_OFFSET + 28] << 8) | snapdata[SNX_OFFSET + 27];
1579    LOAD_REG(cpu, Z80_AF, data);
1580
1581    data = (snapdata[SNX_OFFSET + 20] << 8) | snapdata[SNX_OFFSET + 19];
1582    LOAD_REG(cpu, Z80_BC, data);
1583
1584    data = (snapdata[SNX_OFFSET + 18] << 8) | snapdata[SNX_OFFSET + 17];
1585    LOAD_REG(cpu, Z80_DE, data);
1586
1587    data = (snapdata[SNX_OFFSET + 16] << 8) | snapdata[SNX_OFFSET + 15];
1588    LOAD_REG(cpu, Z80_HL, data);
1589
1590
1591    data = (snapdata[SNX_OFFSET + 14] << 8) | snapdata[SNX_OFFSET + 13];
1592    LOAD_REG(cpu, Z80_AF2, data);
1593
1594    data = (snapdata[SNX_OFFSET + 12] << 8) | snapdata[SNX_OFFSET + 11];
1595    LOAD_REG(cpu, Z80_BC2, data);
1596
1597    data = (snapdata[SNX_OFFSET + 10] << 8) | snapdata[SNX_OFFSET +  9];
1598    LOAD_REG(cpu, Z80_DE2, data);
1599
1600    data = (snapdata[SNX_OFFSET +  8] << 8) | snapdata[SNX_OFFSET +  7];
1601    LOAD_REG(cpu, Z80_HL2, data);
1602
1603
1604    data = (snapdata[SNX_OFFSET + 24] << 8) | snapdata[SNX_OFFSET + 23];
1605    LOAD_REG(cpu, Z80_IX, data);
1606
1607    data = (snapdata[SNX_OFFSET + 22] << 8) | snapdata[SNX_OFFSET + 21];
1608    LOAD_REG(cpu, Z80_IY, data);
1609
1610
1611    data = snapdata[SNX_OFFSET + 26];
1612    LOAD_REG(cpu, Z80_R, data);
1613
1614    data = snapdata[SNX_OFFSET +  6];
1615    LOAD_REG(cpu, Z80_I, data);
1616
1617
1618    data = (snapdata[SNX_OFFSET + 30] << 8) | snapdata[SNX_OFFSET + 29];
1619    LOAD_REG(cpu, Z80_SP, data);
1620
1621
1622    data = snapdata[SNX_OFFSET + 31] & 0x03;
1623    if (data == 3)
1624        data = 2;
1625    LOAD_REG(cpu, Z80_IM, data);
1626
1627    data = snapdata[SNX_OFFSET + 25];
1628    LOAD_REG(cpu, Z80_IFF1, BIT(data, 0));
1629    LOAD_REG(cpu, Z80_IFF2, BIT(data, 2));
1630
1631    intr = BIT(data, 0) ? CLEAR_LINE : ASSERT_LINE;
1632    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, intr);
1633    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1634
1635    /* Memory dump */
1636    logerror("Uncompressing %04X bytes of RAM at %04X\n", 3*SPECTRUM_BANK, BASE_RAM);
1637    spectrum_snx_decompress_block(machine, snapdata, BASE_RAM, 3*SPECTRUM_BANK);
1638
1639    /* get pc from stack */
1640    addr = cpu_get_reg(cpu, Z80_SP);
1641
1642    if (addr < BASE_RAM)
1643      logerror("Corrupted SP points to ROM at:%04X", addr);
1644    else
1645      logerror("Fetching PC from the stack at SP:%04X\n", addr);
1646
1647    LOAD_REG(cpu, Z80_PC, (memory_read_byte(space, addr + 1) << 8) | memory_read_byte(space, addr + 0));
1648
1649#if 0
1650    memory_write_byte(space, addr + 0, 0); // It's been reported that zeroing these locations fixes the loading
1651    memory_write_byte(space, addr + 1, 0); // of a few images that were snapshot at a "wrong" instant
1652#endif
1653
1654    addr += 2;
1655    logerror("Fixed the stack at SP:%04X\n", addr);
1656    cpu_set_reg(cpu, Z80_SP, addr);
1657
1658    /* Set border color */
1659    data = snapdata[SNX_OFFSET + 32] & 0x07;
1660    state->port_fe_data = (state->port_fe_data & 0xf8) | data;
1661    EventList_Reset();
1662    border_set_last_color(data);
1663    border_force_redraw();
1664    logerror("Border color:%02X\n", data);
1665
1666    spectrum_page_basicrom(machine);
1667
1668/* TODO: Enable/disable IF1 as per snapdata[SNX_OFFSET + 33] */
1669
1670/* TODO: Enable/disable joysticks as per snapdata[SNX_OFFSET + 36] */
1671
1672    logerror("Snapshot loaded.\nExecution resuming at %s\n", cpu_get_reg_string(cpu, Z80_PC));
1673}
1674
1675static void spectrum_z80_decompress_block(running_machine *machine,UINT8 *pSource, int Dest, int size)
1676{
1677    UINT8 ch;
1678    int i;
1679    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1680
1681    do
1682    {
1683        /* get byte */
1684        ch = pSource[0];
1685
1686        /* either start 0f 0x0ed, 0x0ed, xx yy or
1687         * single 0x0ed */
1688        if (ch == (UINT8) 0x0ed)
1689        {
1690            if (pSource[1] == (UINT8) 0x0ed)
1691            {
1692
1693                /* 0x0ed, 0x0ed, xx yy */
1694                /* repetition */
1695
1696                int count;
1697                int data;
1698
1699                count = (pSource[2] & 0x0ff);
1700
1701                if (count == 0)
1702                    return;
1703
1704                data = (pSource[3] & 0x0ff);
1705
1706                pSource += 4;
1707
1708                if (count > size)
1709                    count = size;
1710
1711                size -= count;
1712
1713                for (i = 0; i < count; i++)
1714                {
1715                    memory_write_byte(space,Dest, data);
1716                    Dest++;
1717                }
1718            }
1719            else
1720            {
1721                /* single 0x0ed */
1722                memory_write_byte(space,Dest, ch);
1723                Dest++;
1724                pSource++;
1725                size--;
1726
1727            }
1728        }
1729        else
1730        {
1731            /* not 0x0ed */
1732            memory_write_byte(space,Dest, ch);
1733            Dest++;
1734            pSource++;
1735            size--;
1736        }
1737
1738    }
1739    while (size > 0);
1740}
1741
1742static SPECTRUM_Z80_SNAPSHOT_TYPE spectrum_identify_z80 (UINT8 *snapdata, UINT32 snapsize)
1743{
1744    UINT8 lo, hi, data;
1745
1746    if (snapsize < 30)
1747        return SPECTRUM_Z80_SNAPSHOT_INVALID;   /* Invalid file */
1748
1749    lo = snapdata[6] & 0x0ff;
1750    hi = snapdata[7] & 0x0ff;
1751    if (hi || lo)
1752        return SPECTRUM_Z80_SNAPSHOT_48K_OLD;   /* V1.45 - 48K only */
1753
1754    lo = snapdata[30] & 0x0ff;
1755    hi = snapdata[31] & 0x0ff;
1756    data = snapdata[34] & 0x0ff;            /* Hardware mode */
1757
1758    if ((hi == 0) && (lo == 23))
1759    {                       /* V2.01 */                         /* V2.01 format */
1760        switch (data)
1761        {
1762            case 0:
1763            case 1: return SPECTRUM_Z80_SNAPSHOT_48K;
1764            case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
1765            case 3:
1766            case 4: return SPECTRUM_Z80_SNAPSHOT_128K;
1767            case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
1768        }
1769    }
1770
1771    if ((hi == 0) && (lo == 54))
1772    {                       /* V3.0x */                         /* V2.01 format */
1773        switch (data)
1774        {
1775            case 0:
1776            case 1:
1777            case 3: return SPECTRUM_Z80_SNAPSHOT_48K;
1778            case 2: return SPECTRUM_Z80_SNAPSHOT_SAMRAM;
1779            case 4:
1780            case 5:
1781            case 6: return SPECTRUM_Z80_SNAPSHOT_128K;
1782            case 128: return SPECTRUM_Z80_SNAPSHOT_TS2068;
1783        }
1784    }
1785
1786    return SPECTRUM_Z80_SNAPSHOT_INVALID;
1787}
1788
1789/* now supports 48k & 128k .Z80 files */
1790void spectrum_setup_z80(running_machine *machine, UINT8 *snapdata, UINT32 snapsize)
1791{
1792    spectrum_state *state = (spectrum_state *)machine->driver_data;
1793    int i;
1794    UINT8 lo, hi, data;
1795    SPECTRUM_Z80_SNAPSHOT_TYPE z80_type;
1796    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1797
1798    z80_type = spectrum_identify_z80(snapdata, snapsize);
1799
1800    switch (z80_type)
1801    {
1802        case SPECTRUM_Z80_SNAPSHOT_INVALID:
1803                logerror("Invalid .Z80 file\n");
1804                return;
1805        case SPECTRUM_Z80_SNAPSHOT_48K_OLD:
1806        case SPECTRUM_Z80_SNAPSHOT_48K:
1807                logerror("48K .Z80 file\n");
1808                if (!strcmp(machine->gamedrv->name,"ts2068"))
1809                    logerror("48K .Z80 file in TS2068\n");
1810                break;
1811        case SPECTRUM_Z80_SNAPSHOT_128K:
1812                logerror("128K .Z80 file\n");
1813                if (state->port_7ffd_data == -1)
1814                {
1815                    logerror("Not a 48K .Z80 file\n");
1816                    return;
1817                }
1818                if (!strcmp(machine->gamedrv->name,"ts2068"))
1819                {
1820                    logerror("Not a TS2068 .Z80 file\n");
1821                    return;
1822                }
1823                break;
1824        case SPECTRUM_Z80_SNAPSHOT_TS2068:
1825                logerror("TS2068 .Z80 file\n");
1826                if (strcmp(machine->gamedrv->name,"ts2068"))
1827                    logerror("Not a TS2068 machine\n");
1828                break;
1829        case SPECTRUM_Z80_SNAPSHOT_SAMRAM:
1830                logerror("Hardware not supported - .Z80 file\n");
1831                return;
1832    }
1833
1834    /* AF */
1835    hi = snapdata[0] & 0x0ff;
1836    lo = snapdata[1] & 0x0ff;
1837    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_AF, (hi << 8) | lo);
1838    /* BC */
1839    lo = snapdata[2] & 0x0ff;
1840    hi = snapdata[3] & 0x0ff;
1841    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_BC, (hi << 8) | lo);
1842    /* HL */
1843    lo = snapdata[4] & 0x0ff;
1844    hi = snapdata[5] & 0x0ff;
1845    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_HL, (hi << 8) | lo);
1846
1847    /* SP */
1848    lo = snapdata[8] & 0x0ff;
1849    hi = snapdata[9] & 0x0ff;
1850    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_SP, (hi << 8) | lo);
1851
1852    /* I */
1853    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_I, (snapdata[10] & 0x0ff));
1854
1855    /* R */
1856    data = (snapdata[11] & 0x07f) | ((snapdata[12] & 0x01) << 7);
1857    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_R, data);
1858
1859    /* Set border color */
1860    state->port_fe_data = (state->port_fe_data & 0xf8) | ((snapdata[12] & 0x0e) >> 1);
1861    EventList_Reset();
1862    border_set_last_color((snapdata[12] & 0x0e) >> 1);
1863    border_force_redraw();
1864
1865    lo = snapdata[13] & 0x0ff;
1866    hi = snapdata[14] & 0x0ff;
1867    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_DE, (hi << 8) | lo);
1868
1869    lo = snapdata[15] & 0x0ff;
1870    hi = snapdata[16] & 0x0ff;
1871    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_BC2, (hi << 8) | lo);
1872
1873    lo = snapdata[17] & 0x0ff;
1874    hi = snapdata[18] & 0x0ff;
1875    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_DE2, (hi << 8) | lo);
1876
1877    lo = snapdata[19] & 0x0ff;
1878    hi = snapdata[20] & 0x0ff;
1879    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_HL2, (hi << 8) | lo);
1880
1881    hi = snapdata[21] & 0x0ff;
1882    lo = snapdata[22] & 0x0ff;
1883    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_AF2, (hi << 8) | lo);
1884
1885    lo = snapdata[23] & 0x0ff;
1886    hi = snapdata[24] & 0x0ff;
1887    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IY, (hi << 8) | lo);
1888
1889    lo = snapdata[25] & 0x0ff;
1890    hi = snapdata[26] & 0x0ff;
1891    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IX, (hi << 8) | lo);
1892
1893    /* Interrupt Flip/Flop */
1894    if (snapdata[27] == 0)
1895    {
1896        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF1, 0);
1897        /* cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IRQ_STATE, 0); */
1898    }
1899    else
1900    {
1901        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF1, 1);
1902        /* cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IRQ_STATE, 1); */
1903    }
1904
1905    cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, data);
1906//  cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, data);
1907    cputag_set_input_line(machine, "maincpu", INPUT_LINE_HALT, CLEAR_LINE);
1908
1909    /* IFF2 */
1910    if (snapdata[28] != 0)
1911    {
1912        data = 1;
1913    }
1914    else
1915    {
1916        data = 0;
1917    }
1918    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IFF2, data);
1919
1920    /* Interrupt Mode */
1921    cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_IM, (snapdata[29] & 0x03));
1922
1923    if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K_OLD)
1924    {
1925        lo = snapdata[6] & 0x0ff;
1926        hi = snapdata[7] & 0x0ff;
1927        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_PC, (hi << 8) | lo);
1928
1929        spectrum_page_basicrom(machine);
1930
1931        if ((snapdata[12] & 0x020) == 0)
1932        {
1933            logerror("Not compressed\n");   /* not compressed */
1934            for (i = 0; i < 49152; i++)
1935                memory_write_byte(space,i + 16384, snapdata[30 + i]);
1936        }
1937        else
1938        {
1939            logerror("Compressed\n");   /* compressed */
1940            spectrum_z80_decompress_block(machine, snapdata + 30, 16384, 49152);
1941        }
1942    }
1943    else
1944    {
1945        UINT8 *pSource;
1946        int header_size;
1947
1948        header_size = 30 + 2 + ((snapdata[30] & 0x0ff) | ((snapdata[31] & 0x0ff) << 8));
1949
1950        lo = snapdata[32] & 0x0ff;
1951        hi = snapdata[33] & 0x0ff;
1952        cpu_set_reg(devtag_get_device(machine, "maincpu"), Z80_PC, (hi << 8) | lo);
1953
1954        if ((z80_type == SPECTRUM_Z80_SNAPSHOT_128K) || ((z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068) && !strcmp(machine->gamedrv->name,"ts2068")))
1955        {
1956            running_device *ay8912 = devtag_get_device(machine, "ay8912");
1957
1958            /* Only set up sound registers for 128K machine or TS2068! */
1959            for (i = 0; i < 16; i++)
1960            {
1961                ay8910_address_w(ay8912, 0, i);
1962                ay8910_data_w(ay8912, 0, snapdata[39 + i]);
1963            }
1964            ay8910_address_w(ay8912, 0, snapdata[38]);
1965        }
1966
1967        pSource = snapdata + header_size;
1968
1969        if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K)
1970            /* Ensure 48K Basic ROM is used */
1971            spectrum_page_basicrom(machine);
1972
1973        do
1974        {
1975            unsigned short length;
1976            UINT8 page;
1977            int Dest = 0;
1978
1979            length = (pSource[0] & 0x0ff) | ((pSource[1] & 0x0ff) << 8);
1980            page = pSource[2];
1981
1982            if (z80_type == SPECTRUM_Z80_SNAPSHOT_48K || z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068)
1983            {
1984                switch (page)
1985                {
1986                    case 4: Dest = 0x08000; break;
1987                    case 5: Dest = 0x0c000; break;
1988                    case 8: Dest = 0x04000; break;
1989                    default: Dest = 0; break;
1990                }
1991            }
1992            else
1993            {
1994                /* 3 = bank 0, 4 = bank 1 ... 10 = bank 7 */
1995                if ((page >= 3) && (page <= 10))
1996                {
1997                    /* Page the appropriate bank into 0xc000 - 0xfff */
1998                    state->port_7ffd_data = page - 3;
1999                    spectrum_update_paging(machine);
2000                    Dest = 0x0c000;
2001                }
2002                else
2003                    /* Other values correspond to ROM pages */
2004                    Dest = 0x0;
2005            }
2006
2007            if (Dest != 0)
2008            {
2009                if (length == 0x0ffff)
2010                {
2011                    /* block is uncompressed */
2012                    logerror("Not compressed\n");
2013
2014                    /* not compressed */
2015                    for (i = 0; i < 16384; i++)
2016                        memory_write_byte(space,i + Dest, pSource[i]);
2017                }
2018                else
2019                {
2020                    logerror("Compressed\n");
2021
2022                    /* block is compressed */
2023                    spectrum_z80_decompress_block(machine,&pSource[3], Dest, 16384);
2024                }
2025            }
2026
2027            /* go to next block */
2028            pSource += (3 + length);
2029        }
2030        while ((pSource - snapdata) < snapsize);
2031
2032        if ((state->port_7ffd_data != -1) && (z80_type != SPECTRUM_Z80_SNAPSHOT_48K))
2033        {
2034            /* Set up paging */
2035            state->port_7ffd_data = (snapdata[35] & 0x0ff);
2036            spectrum_update_paging(machine);
2037        }
2038        if ((z80_type == SPECTRUM_Z80_SNAPSHOT_48K) && !strcmp(machine->gamedrv->name,"ts2068"))
2039        {
2040            state->port_f4_data = 0x03;
2041            state->port_ff_data = 0x00;
2042            ts2068_update_memory(machine);
2043        }
2044        if (z80_type == SPECTRUM_Z80_SNAPSHOT_TS2068 && !strcmp(machine->gamedrv->name,"ts2068"))
2045        {
2046            state->port_f4_data = snapdata[35];
2047            state->port_ff_data = snapdata[36];
2048            ts2068_update_memory(machine);
2049        }
2050    }
2051}
2052
2053QUICKLOAD_LOAD(spectrum)
2054{
2055    const address_space *space = cputag_get_address_space(image->machine, "maincpu", ADDRESS_SPACE_PROGRAM);
2056    int i;
2057    int quick_addr = BASE_RAM;
2058    int quick_length;
2059    UINT8 *quick_data;
2060    int read_;
2061
2062    quick_length = image_length(image);
2063    quick_data = (UINT8 *)malloc(quick_length);
2064    if (!quick_data)
2065        return INIT_FAIL;
2066
2067    read_ = image_fread(image, quick_data, quick_length);
2068    if (read_ != quick_length)
2069        return INIT_FAIL;
2070
2071    for (i = 0; i < quick_length; i++)
2072        memory_write_byte(space,i + quick_addr, quick_data[i]);
2073
2074    logerror("quick loading at %.4x size:%.4x\n", quick_addr, quick_length);
2075    return INIT_PASS;
20762076}
No newline at end of file
src/mess/formats/spec_snqk.h
r7380r7381
1/*****************************************************************************
2 *
3 * formats/spec_snqk.h
4 *
5 ****************************************************************************/
6 
7#ifndef __SPEC_SNQK_H__
8#define __SPEC_SNQK_H__
9 
10#include "devices/snapquik.h"
11#include "devices/cartslot.h"
12 
13void spectrum_setup_sna(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
14void spectrum_setup_z80(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
15void spectrum_setup_sp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
16void spectrum_setup_ach(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
17void spectrum_setup_prg(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
18void spectrum_setup_sem(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
19void spectrum_setup_sit(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
20void spectrum_setup_zx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
21void spectrum_setup_snp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
22void spectrum_setup_snx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
23 
24#define BASE_RAM      0x4000
25#define SPECTRUM_BANK 0x4000
26 
27/*****************************************************************************
28 *
29 * .SNA format (used by JPP)
30 *
31 ****************************************************************************/
32#define SNA48_OFFSET  0
33#define SNA48_HDR     27
34#define SNA48_SIZE    (SNA48_HDR + 3*SPECTRUM_BANK)
35 
36#define SNA128_OFFSET (SNA48_HDR + 3*SPECTRUM_BANK)
37#define SNA128_HDR    4
38#define SNA128_SIZE_1 (SNA48_HDR + 3*SPECTRUM_BANK + SNA128_HDR + 5*SPECTRUM_BANK)
39#define SNA128_SIZE_2 (SNA48_HDR + 3*SPECTRUM_BANK + SNA128_HDR + 6*SPECTRUM_BANK)
40 
41/*****************************************************************************
42 *
43 * .SP format (used by VGASPEC and Spanish Spectrum emulator)
44 *
45 ****************************************************************************/
46#define SP_OLD_HDR      32
47#define SP_NEW_HDR      38
48#define SP_OLD_SIZE     (SP_OLD_HDR + 3*SPECTRUM_BANK)
49#define SP_NEW_SIZE_16K (SP_NEW_HDR + 1*SPECTRUM_BANK)
50#define SP_NEW_SIZE_48K (SP_NEW_HDR + 3*SPECTRUM_BANK)
51 
52/*****************************************************************************
53 *
54 * .ACH format (used by !Speccy)
55 *
56 ****************************************************************************/
57#define ACH_OFFSET 0
58#define ACH_HDR    256
59#define ACH_SIZE   (ACH_HDR + BASE_RAM + 3*SPECTRUM_BANK)
60 
61/*****************************************************************************
62 *
63 * .PRG format (used by SpecEm)
64 *
65 ****************************************************************************/
66#define PRG_OFFSET 0
67#define PRG_HDR    256
68#define PRG_SIZE   (PRG_HDR + 3*SPECTRUM_BANK)
69 
70/*****************************************************************************
71 *
72 * .SEM format (used by SpecEmu)
73 *
74 ****************************************************************************/
75#define SEM_SIGNATURE 6
76#define SEM_OFFSET    49158
77#define SEM_HDR       34
78#define SEM_SIZE      (SEM_SIGNATURE + 3*SPECTRUM_BANK + SEM_HDR)
79 
80/*****************************************************************************
81 *
82 * .SIT format (used by Sinclair)
83 *
84 ****************************************************************************/
85#define SIT_OFFSET 0
86#define SIT_HDR    28
87#define SIT_SIZE   (SIT_HDR + BASE_RAM + 3*SPECTRUM_BANK)
88 
89/*****************************************************************************
90 *
91 * .ZX format (used by KGB)
92 *
93 ****************************************************************************/
94#define ZX_OFFSET 49284
95#define ZX_HDR    202
96#define ZX_SIZE   (132 + 3*SPECTRUM_BANK + ZX_HDR)
97 
98/*****************************************************************************
99 *
100 * .SNP format (used by Nuclear ZX)
101 *
102 ****************************************************************************/
103#define SNP_OFFSET 49152
104#define SNP_HDR    31
105#define SNP_SIZE   (3*SPECTRUM_BANK + SNP_HDR)
106 
107/*****************************************************************************
108 *
109 * .SNX format (used by Specci)
110 *
111 ****************************************************************************/
112#define SNX_OFFSET 0
113#define SNX_HDR    43
114 
115#define SNX_COMPRESSED   0xff
116#define SNX_UNCOMPRESSED 0x00
117 
118typedef enum
119{
120    SPECTRUM_SNAPSHOT_NONE,
121    SPECTRUM_SNAPSHOT_SNA,
122    SPECTRUM_SNAPSHOT_Z80,
123    SPECTRUM_SNAPSHOT_SP,
124    SPECTRUM_TAPEFILE_TAP
125} SPECTRUM_SNAPSHOT_TYPE;
126 
127typedef enum {
128    SPECTRUM_Z80_SNAPSHOT_INVALID,
129    SPECTRUM_Z80_SNAPSHOT_48K_OLD,
130    SPECTRUM_Z80_SNAPSHOT_48K,
131    SPECTRUM_Z80_SNAPSHOT_SAMRAM,
132    SPECTRUM_Z80_SNAPSHOT_128K,
133    SPECTRUM_Z80_SNAPSHOT_TS2068
134} SPECTRUM_Z80_SNAPSHOT_TYPE;
135 
136SNAPSHOT_LOAD( spectrum );
137QUICKLOAD_LOAD( spectrum );
138 
139#endif  /* __SPEC_SNQK_H__ */
1/*****************************************************************************
2 *
3 * formats/spec_snqk.h
4 *
5 ****************************************************************************/
6
7#ifndef __SPEC_SNQK_H__
8#define __SPEC_SNQK_H__
9
10#include "devices/snapquik.h"
11#include "devices/cartslot.h"
12
13void spectrum_setup_sna(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
14void spectrum_setup_z80(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
15void spectrum_setup_sp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
16void spectrum_setup_ach(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
17void spectrum_setup_prg(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
18void spectrum_setup_sem(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
19void spectrum_setup_sit(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
20void spectrum_setup_zx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
21void spectrum_setup_snp(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
22void spectrum_setup_snx(running_machine *machine, UINT8 *snapdata, UINT32 snapsize);
23
24#define BASE_RAM      0x4000
25#define SPECTRUM_BANK 0x4000
26
27/*****************************************************************************
28 *
29 * .SNA format (used by JPP)
30 *
31 ****************************************************************************/
32#define SNA48_OFFSET  0
33#define SNA48_HDR     27
34#define SNA48_SIZE    (SNA48_HDR + 3*SPECTRUM_BANK)
35
36#define SNA128_OFFSET (SNA48_HDR + 3*SPECTRUM_BANK)
37#define SNA128_HDR    4
38#define SNA128_SIZE_1 (SNA48_HDR + 3*SPECTRUM_BANK + SNA128_HDR + 5*SPECTRUM_BANK)
39#define SNA128_SIZE_2 (SNA48_HDR + 3*SPECTRUM_BANK + SNA128_HDR + 6*SPECTRUM_BANK)
40
41/*****************************************************************************
42 *
43 * .SP format (used by VGASPEC and Spanish Spectrum emulator)
44 *
45 ****************************************************************************/
46#define SP_OLD_HDR      32
47#define SP_NEW_HDR      38
48#define SP_OLD_SIZE     (SP_OLD_HDR + 3*SPECTRUM_BANK)
49#define SP_NEW_SIZE_16K (SP_NEW_HDR + 1*SPECTRUM_BANK)
50#define SP_NEW_SIZE_48K (SP_NEW_HDR + 3*SPECTRUM_BANK)
51
52/*****************************************************************************
53 *
54 * .ACH format (used by !Speccy)
55 *
56 ****************************************************************************/
57#define ACH_OFFSET 0
58#define ACH_HDR    256
59#define ACH_SIZE   (ACH_HDR + BASE_RAM + 3*SPECTRUM_BANK)
60
61/*****************************************************************************
62 *
63 * .PRG format (used by SpecEm)
64 *
65 ****************************************************************************/
66#define PRG_OFFSET 0
67#define PRG_HDR    256
68#define PRG_SIZE   (PRG_HDR + 3*SPECTRUM_BANK)
69
70/*****************************************************************************
71 *
72 * .SEM format (used by SpecEmu)
73 *
74 ****************************************************************************/
75#define SEM_SIGNATURE 6
76#define SEM_OFFSET    49158
77#define SEM_HDR       34
78#define SEM_SIZE      (SEM_SIGNATURE + 3*SPECTRUM_BANK + SEM_HDR)
79
80/*****************************************************************************
81 *
82 * .SIT format (used by Sinclair)
83 *
84 ****************************************************************************/
85#define SIT_OFFSET 0
86#define SIT_HDR    28
87#define SIT_SIZE   (SIT_HDR + BASE_RAM + 3*SPECTRUM_BANK)
88
89/*****************************************************************************
90 *
91 * .ZX format (used by KGB)
92 *
93 ****************************************************************************/
94#define ZX_OFFSET 49284
95#define ZX_HDR    202
96#define ZX_SIZE   (132 + 3*SPECTRUM_BANK + ZX_HDR)
97
98/*****************************************************************************
99 *
100 * .SNP format (used by Nuclear ZX)
101 *
102 ****************************************************************************/
103#define SNP_OFFSET 49152
104#define SNP_HDR    31
105#define SNP_SIZE   (3*SPECTRUM_BANK + SNP_HDR)
106
107/*****************************************************************************
108 *
109 * .SNX format (used by Specci)
110 *
111 ****************************************************************************/
112#define SNX_OFFSET 0
113#define SNX_HDR    43
114
115#define SNX_COMPRESSED   0xff
116#define SNX_UNCOMPRESSED 0x00
117
118typedef enum
119{
120    SPECTRUM_SNAPSHOT_NONE,
121    SPECTRUM_SNAPSHOT_SNA,
122    SPECTRUM_SNAPSHOT_Z80,
123    SPECTRUM_SNAPSHOT_SP,
124    SPECTRUM_TAPEFILE_TAP
125} SPECTRUM_SNAPSHOT_TYPE;
126
127typedef enum {
128    SPECTRUM_Z80_SNAPSHOT_INVALID,
129    SPECTRUM_Z80_SNAPSHOT_48K_OLD,
130    SPECTRUM_Z80_SNAPSHOT_48K,
131    SPECTRUM_Z80_SNAPSHOT_SAMRAM,
132    SPECTRUM_Z80_SNAPSHOT_128K,
133    SPECTRUM_Z80_SNAPSHOT_TS2068
134} SPECTRUM_Z80_SNAPSHOT_TYPE;
135
136SNAPSHOT_LOAD( spectrum );
137QUICKLOAD_LOAD( spectrum );
138
139#endif  /* __SPEC_SNQK_H__ */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team