1 /* z80em.c: Routines for handling Z80Em raw audio files
2    Copyright (c) 2002 Darren Salt
3    Based on tap.c, copyright (c) 2001 Philip Kendall
4 
5    $Id: z80em.c 3708 2008-07-01 08:07:01Z pak21 $
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License along
18    with this program; if not, write to the Free Software Foundation, Inc.,
19    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21    Author contact information:
22 
23    E-mail: linux@youmustbejoking.demon.co.uk
24 
25 */
26 
27 #include <config.h>
28 #include <string.h>
29 
30 #include "internals.h"
31 #include "tape_block.h"
32 
33 libspectrum_error
libspectrum_z80em_read(libspectrum_tape * tape,const libspectrum_byte * buffer,size_t length)34 libspectrum_z80em_read( libspectrum_tape *tape,
35 			const libspectrum_byte *buffer, size_t length )
36 {
37   libspectrum_tape_block *block;
38   libspectrum_tape_rle_pulse_block *z80em_block;
39 
40   static const char id[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0Raw tape sample";
41 
42   if( length < sizeof( id ) ) {
43     libspectrum_print_error(
44       LIBSPECTRUM_ERROR_CORRUPT,
45       "libspectrum_z80em_read: not enough data in buffer"
46     );
47     return LIBSPECTRUM_ERROR_CORRUPT;
48   }
49 
50   if( memcmp( id, buffer, sizeof( id ) ) ) {
51     libspectrum_print_error( LIBSPECTRUM_ERROR_SIGNATURE,
52 			     "libspectrum_z80em_read: wrong signature" );
53     return LIBSPECTRUM_ERROR_SIGNATURE;
54   }
55 
56   block = libspectrum_tape_block_alloc( LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE );
57 
58   z80em_block = &block->types.rle_pulse;
59   z80em_block->scale = 7; /* 1 time unit == 7 clock ticks */
60 
61   buffer += sizeof( id );
62   length -= sizeof( id );
63 
64   /* Claim memory for the data (it's one big lump) */
65   z80em_block->length = length;
66   z80em_block->data = libspectrum_malloc( length );
67 
68   /* Copy the data across */
69   memcpy( z80em_block->data, buffer, length );
70 
71   libspectrum_tape_append_block( tape, block );
72 
73   return LIBSPECTRUM_ERROR_NONE;
74 }
75