-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsg.c
172 lines (125 loc) · 3.59 KB
/
psg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* psg.c: recording AY chip output to .psg files
Copyright (c) 2003 Matthew Westcott, Philip Kendall
$Id: psg.c 2889 2007-05-26 17:45:08Z zubzero $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: [email protected]
*/
#include <config.h>
#include <stdio.h>
#include "psg.h"
#include "ui/ui.h"
/* Are we currently recording a .psg file? */
int psg_recording;
static libspectrum_byte psg_register_values[ AY_REGISTERS ];
/* booleans to indicate the registers written to in this frame */
static int psg_registers_written[ AY_REGISTERS ];
/* number of prior frames with no AY events */
static int psg_empty_frame_count;
static FILE *psg_file;
static int write_frame_separator( void );
int
psg_init( void )
{
psg_recording = 0;
return 0;
}
int
psg_start_recording( const char *filename )
{
int i;
if( psg_recording ) return 1;
psg_file = fopen( filename, "wb" );
if( psg_file == NULL ) {
ui_error( UI_ERROR_ERROR, "unable to open PSG file for writing" );
return 1;
}
/* write PSG file header */
if( fprintf( psg_file, "PSG\x1a" ) < 0 ) {
ui_error( UI_ERROR_ERROR, "unable to write PSG file header" );
return 1;
}
for( i = 0; i < 12; i++ ) putc( 0, psg_file );
/* begin with no registers written */
for( i = 0; i < AY_REGISTERS; i++ ) psg_registers_written[i] = 0;
psg_empty_frame_count = 1;
psg_recording = 1;
return 0;
}
int
psg_stop_recording( void )
{
if( !psg_recording ) return 1;
/* flush final frame */
psg_frame();
/* end file with the appropriate end-frame marker */
write_frame_separator();
fclose( psg_file );
psg_recording = 0;
return 0;
}
static int
write_frame_separator( void )
{
while( psg_empty_frame_count >= 4 ) {
int count;
count = psg_empty_frame_count / 4;
if( count > 0xff ) count = 0xff;
putc( 0xfe, psg_file );
putc( count, psg_file );
psg_empty_frame_count -= 4 * count;
}
for( ; psg_empty_frame_count; psg_empty_frame_count-- )
putc( 0xff, psg_file );
return 0;
}
int
psg_frame( void )
{
int i;
int ay_updated;
if( !psg_recording ) return 0;
/* check if any AY sound events have happened this frame */
ay_updated = 0;
for( i = 0; i < 14 && !ay_updated; i++ )
ay_updated = psg_registers_written[i];
if( ay_updated ) {
write_frame_separator();
for( i = 0; i < 14; i++ ) {
if( psg_registers_written[i] ) {
putc( i, psg_file );
putc( psg_register_values[i], psg_file );
}
}
psg_empty_frame_count = 1;
} else {
/* AY not updated */
psg_empty_frame_count++;
}
for( i = 0; i < AY_REGISTERS; i++ ) psg_registers_written[i] = 0;
return 0;
}
int
psg_write_register( libspectrum_byte reg, libspectrum_byte value )
{
if( psg_registers_written[reg] ) return 0;
psg_register_values[reg] = value;
psg_registers_written[reg] = 1;
return 0;
}
int
psg_end( void )
{
if( psg_recording ) return psg_stop_recording();
return 0;
}