Previous 15994 Revisions Next

r7491 Friday 5th March, 2010 at 00:24:52 UTC by Stefano Priore
--
[src/mess/formats]m65_snqk.c* m65_snqk.h*

src/mess/formats/m65_snqk.c
r0r7491
1/******************************************************************************
2 *  Microtan 65
3 *
4 *  Snapshot and quickload formats
5 *
6 *  Juergen Buchmueller <pullmoll@t-online.de>, Jul 2000
7 *
8 *  Thanks go to Geoff Macdonald <mail@geoff.org.uk>
9 *  for his site http:://www.geo255.redhotant.com
10 *  and to Fabrice Frances <frances@ensica.fr>
11 *  for his site http://www.ifrance.com/oric/microtan.html
12 *
13 *****************************************************************************/
14#include "emu.h"
15#include "cpu/m6502/m6502.h"
16#include "includes/microtan.h"
17#include "machine/6522via.h"
18#include "machine/6551.h"
19#include "sound/ay8910.h"
20
21static int microtan_verify_snapshot(UINT8 *data, int size)
22{
23    if (size == 8263)
24    {
25        logerror("microtan_snapshot_id: magic size %d found\n", size);
26        return IMAGE_VERIFY_PASS;
27    }
28    else
29    {
30        if (4 + data[2] + 256 * data[3] + 1 + 16 + 16 + 16 + 1 + 1 + 16 + 16 + 64 + 7 == size)
31        {
32            logerror("microtan_snapshot_id: header RAM size + structures matches filesize %d\n", size);
33            return IMAGE_VERIFY_PASS;
34        }
35    }
36
37    return IMAGE_VERIFY_FAIL;
38}
39
40static int parse_intel_hex(UINT8 *snapshot_buff, char *src)
41{
42    char line[128];
43    int /*row = 0,*/ column = 0, last_addr = 0, last_size = 0;
44
45    while (*src)
46    {
47        if (*src == '\r' || *src == '\n')
48        {
49            if (column)
50            {
51                unsigned int size, addr, null, b[32], cs, n;
52
53                line[column] = '\0';
54                /*row++;*/
55                n = sscanf(line, ":%02x%04x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
56                    &size, &addr, &null,
57                    &b[ 0], &b[ 1], &b[ 2], &b[ 3], &b[ 4], &b[ 5], &b[ 6], &b[ 7],
58                    &b[ 8], &b[ 9], &b[10], &b[11], &b[12], &b[13], &b[14], &b[15],
59                    &b[16], &b[17], &b[18], &b[19], &b[20], &b[21], &b[22], &b[23],
60                    &b[24], &b[25], &b[26], &b[27], &b[28], &b[29], &b[30], &b[31],
61                    &cs);
62                if (n == 0)
63                {
64                    logerror("parse_intel_hex: malformed line [%s]\n", line);
65                }
66                else if (n == 1)
67                {
68                    logerror("parse_intel_hex: only size found [%s]\n", line);
69                }
70                else if (n == 2)
71                {
72                    logerror("parse_intel_hex: only size and addr found [%s]\n", line);
73                }
74                else if (n == 3)
75                {
76                    logerror("parse_intel_hex: only size, addr and null found [%s]\n", line);
77                }
78                else
79                if (null != 0)
80                {
81                    logerror("parse_intel_hex: warning null byte is != 0 [%s]\n", line);
82                }
83                else
84                {
85                    int i, sum;
86
87                    n -= 3;
88
89                    sum = size + (addr & 0xff) + ((addr >> 8) & 0xff);
90                    if (n != 32 + 1)
91                        cs = b[n-1];
92
93                    last_addr = addr;
94                    last_size = n-1;
95                    logerror("parse_intel_hex: %04X", addr);
96                    for (i = 0; i < n-1; i++)
97                    {
98                        sum += b[i];
99                        snapshot_buff[addr++] = b[i];
100                    }
101                    logerror("-%04X checksum %02X+%02X = %02X\n", addr-1, cs, sum & 0xff, (cs + sum) & 0xff);
102                }
103            }
104            column = 0;
105        }
106        else
107        {
108            line[column++] = *src;
109        }
110        src++;
111    }
112    /* register preset? */
113    if (last_size == 7)
114    {
115        logerror("parse_intel_hex: registers (?) at %04X\n", last_addr);
116        memcpy(&snapshot_buff[8192+64], &snapshot_buff[last_addr], last_size);
117    }
118    return INIT_PASS;
119}
120
121static int parse_zillion_hex(UINT8 *snapshot_buff, char *src)
122{
123    char line[128];
124    int parsing = 0, /*row = 0,*/ column = 0;
125
126    while (*src)
127    {
128        if (parsing)
129        {
130            if (*src == '}')
131                parsing = 0;
132            else
133            {
134                if (*src == '\r' || *src == '\n')
135                {
136                    if (column)
137                    {
138                        unsigned int addr, b[8], n;
139
140                        line[column] = '\0';
141                        /*row++;*/
142                        n = sscanf(line, "%x %x %x %x %x %x %x %x %x", &addr, &b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6], &b[7]);
143                        if (n == 0)
144                        {
145                            logerror("parse_zillion_hex: malformed line [%s]\n", line);
146                        }
147                        else if (n == 1)
148                        {
149                            logerror("parse_zillion_hex: only addr found [%s]\n", line);
150                        }
151                        else
152                        {
153                            int i;
154
155                            logerror("parse_zillion_hex: %04X", addr);
156                            for (i = 0; i < n-1; i++)
157                                snapshot_buff[addr++] = b[i];
158                            logerror("-%04X\n", addr-1);
159                        }
160                    }
161                    column = 0;
162                }
163                else
164                {
165                    line[column++] = *src;
166                }
167            }
168        }
169        else
170        {
171            if (*src == '\r' || *src == '\n')
172            {
173                if (column)
174                {
175                    int addr, n;
176
177                    /*row++;*/
178                    line[column] = '\0';
179                    n = sscanf(line, "G%x", (unsigned int *) &addr);
180                    if (n == 1 && !snapshot_buff[8192+64+1] && !snapshot_buff[8192+64+1])
181                    {
182                        logerror("microtan_hexfile_init: go addr %04X\n", addr);
183                        snapshot_buff[8192+64+0] = addr & 0xff;
184                        snapshot_buff[8192+64+1] = (addr >> 8) & 0xff;
185                    }
186                }
187                column = 0;
188            }
189            else
190            {
191                line[column++] = *src;
192            }
193            if (*src == '{')
194            {
195                parsing = 1;
196                column = 0;
197            }
198        }
199        src++;
200    }
201    return INIT_PASS;
202}
203
204static void microtan_set_cpu_regs(running_machine *machine,const UINT8 *snapshot_buff, int base)
205{
206    logerror("microtan_snapshot_copy: PC:%02X%02X P:%02X A:%02X X:%02X Y:%02X SP:1%02X",
207        snapshot_buff[base+1], snapshot_buff[base+0], snapshot_buff[base+2], snapshot_buff[base+3],
208        snapshot_buff[base+4], snapshot_buff[base+5], snapshot_buff[base+6]);
209    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_PC, snapshot_buff[base+0] + 256 * snapshot_buff[base+1]);
210    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_P, snapshot_buff[base+2]);
211    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_A, snapshot_buff[base+3]);
212    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_X, snapshot_buff[base+4]);
213    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_Y, snapshot_buff[base+5]);
214    cpu_set_reg(devtag_get_device(machine, "maincpu"), M6502_S, snapshot_buff[base+6]);
215}
216
217static void microtan_snapshot_copy(running_machine *machine, UINT8 *snapshot_buff, int snapshot_size)
218{
219    UINT8 *RAM = memory_region(machine, "maincpu");
220    const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
221    running_device *via_0 = devtag_get_device(machine, "via6522_0");
222    running_device *via_1 = devtag_get_device(machine, "via6522_1");
223    running_device *ay8910 = devtag_get_device(machine, "ay8910.1");
224
225    /* check for .DMP file format */
226    if (snapshot_size == 8263)
227    {
228        int i, base;
229        /********** DMP format
230         * Lower 8k of RAM (0000 to 1fff)
231         * 64 bytes of chunky graphics bits (first byte bit is for character at 0200, bit 1=0201, etc)
232         * 7 bytes of CPU registers (PCL, PCH, PSW, A, IX, IY, SP)
233         */
234        logerror("microtan_snapshot_copy: magic size %d found, assuming *.DMP format\n", snapshot_size);
235
236        base = 0;
237        /* 8K of RAM from 0000 to 1fff */
238        memcpy(RAM, &snapshot_buff[base], 8192);
239        base += 8192;
240        /* 64 bytes of chunky graphics info */
241        for (i = 0; i < 32*16; i++)
242        {
243            microtan_chunky_buffer[i] = (snapshot_buff[base+i/8] >> (i&7)) & 1;
244        }
245        base += 64;
246        microtan_set_cpu_regs(machine, snapshot_buff, base);
247    }
248    else
249    {
250        int i, ramend, base;
251        /********** M65 format ************************************
252         *  2 bytes: File version
253         *  2 bytes: RAM size
254         *  n bytes: RAM (0000 to RAM Size)
255         * 16 bytes: 1st 6522 (0xbfc0 to 0xbfcf)
256         * 16 bytes: 2ns 6522 (0xbfe0 to 0xbfef)
257         * 16 bytes: Microtan IO (0xbff0 to 0xbfff)
258         *  1 byte : Invaders sound (0xbc04)
259         *  1 byte : Chunky graphics state (0=off, 1=on)
260         * 16 bytes: 1st AY8910 registers
261         * 16 bytes: 2nd AY8910 registers
262         * 64 bytes: Chunky graphics bits (first byte bit 0 is for character at 0200, bit 1=0201, etc)
263         *  7 bytes: CPU registers (PCL, PCH, PSW, A, IX, IY, SP)
264         */
265        ramend = snapshot_buff[2] + 256 * snapshot_buff[3];
266        if (2 + 2 + ramend + 1 + 16 + 16 + 16 + 1 + 1 + 16 + 16 + 64 + 7 != snapshot_size)
267        {
268            logerror("microtan_snapshot_copy: size %d doesn't match RAM size %d + structure size\n", snapshot_size, ramend+1);
269            return;
270        }
271
272        logerror("microtan_snapshot_copy: size %d found, assuming *.M65 format\n", snapshot_size);
273        base = 4;
274        memcpy(RAM, &snapshot_buff[base], snapshot_buff[2] + 256 * snapshot_buff[3] + 1);
275        base += ramend + 1;
276
277        /* first set of VIA6522 registers */
278        for (i = 0; i < 16; i++ )
279            via_w(via_0, i, snapshot_buff[base++]);
280
281        /* second set of VIA6522 registers */
282        for (i = 0; i < 16; i++ )
283            via_w(via_1, i, snapshot_buff[base++]);
284
285        /* microtan IO bff0-bfff */
286        for (i = 0; i < 16; i++ )
287        {
288            RAM[0xbff0+i] = snapshot_buff[base++];
289            if (i < 4)
290                microtan_bffx_w(space, i, RAM[0xbff0+i]);
291        }
292
293        microtan_sound_w(space, 0, snapshot_buff[base++]);
294        microtan_chunky_graphics = snapshot_buff[base++];
295
296        /* first set of AY8910 registers */
297        for (i = 0; i < 16; i++ )
298        {
299            ay8910_address_w(ay8910, 0, i);
300            ay8910_data_w(ay8910, 0, snapshot_buff[base++]);
301        }
302
303        /* second set of AY8910 registers */
304        for (i = 0; i < 16; i++ )
305        {
306            ay8910_address_w(ay8910, 0, i);
307            ay8910_data_w(ay8910, 0, snapshot_buff[base++]);
308        }
309
310        for (i = 0; i < 32*16; i++)
311        {
312            microtan_chunky_buffer[i] = (snapshot_buff[base+i/8] >> (i&7)) & 1;
313        }
314        base += 64;
315
316        microtan_set_cpu_regs(machine, snapshot_buff, base);
317    }
318}
319
320SNAPSHOT_LOAD( microtan )
321{
322    UINT8 *snapshot_buff;
323
324    snapshot_buff = (UINT8*)image_ptr(image);
325    if (!snapshot_buff)
326        return INIT_FAIL;
327
328    if (microtan_verify_snapshot(snapshot_buff, snapshot_size)==IMAGE_VERIFY_FAIL)
329        return INIT_FAIL;
330
331    microtan_snapshot_copy(image->machine, snapshot_buff, snapshot_size);
332    return INIT_PASS;
333}
334
335QUICKLOAD_LOAD( microtan )
336{
337    int snapshot_size;
338    UINT8 *snapshot_buff;
339    char *buff;
340    int rc;
341
342    snapshot_size = 8263;   /* magic size */
343    snapshot_buff = (UINT8*)malloc(snapshot_size);
344    if (!snapshot_buff)
345    {
346        logerror("microtan_hexfile_load: could not allocate %d bytes of buffer\n", snapshot_size);
347        return INIT_FAIL;
348    }
349    memset(snapshot_buff, 0, snapshot_size);
350
351    buff = (char*)malloc(quickload_size + 1);
352    if (!buff)
353    {
354        free(snapshot_buff);
355        logerror("microtan_hexfile_load: could not allocate %d bytes of buffer\n", quickload_size);
356        return INIT_FAIL;
357    }
358    image_fread(image, buff, quickload_size);
359
360    buff[quickload_size] = '\0';
361
362    if (buff[0] == ':')
363        rc = parse_intel_hex(snapshot_buff, buff);
364    else
365        rc = parse_zillion_hex(snapshot_buff, buff);
366    if (rc == INIT_PASS)
367        microtan_snapshot_copy(image->machine, snapshot_buff, snapshot_size);
368    free(snapshot_buff);
369    return rc;
370}
371
372
src/mess/formats/m65_snqk.h
r0r7491
1/******************************************************************************
2 *  Microtan 65
3 *
4 *  Snapshot and quickload formats
5 *
6 *  Juergen Buchmueller <pullmoll@t-online.de>, Jul 2000
7 *
8 *  Thanks go to Geoff Macdonald <mail@geoff.org.uk>
9 *  for his site http:://www.geo255.redhotant.com
10 *  and to Fabrice Frances <frances@ensica.fr>
11 *  for his site http://www.ifrance.com/oric/microtan.html
12 *
13 *****************************************************************************/
14
15#ifndef __M65_SNQK_H__
16#define __M65_SNQK_H__
17
18#include "devices/snapquik.h"
19
20int microtan_verify_snapshot(UINT8 *data, int size);
21
22SNAPSHOT_LOAD( microtan );
23QUICKLOAD_LOAD( microtan );
24
25#endif /* __M65_SNQK_H__ */
No newline at end of file

Previous 15994 Revisions Next


© 1998-2018 The MESS Team