Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/game-music-emu/gme/Kss_Emu.cpp
Line
Count
Source
1
// Game_Music_Emu https://bitbucket.org/mpyne/game-music-emu/
2
3
#include "Kss_Emu.h"
4
5
#include "blargg_endian.h"
6
#include <string.h>
7
#include <algorithm>
8
9
/* Copyright (C) 2006 Shay Green. This module is free software; you
10
can redistribute it and/or modify it under the terms of the GNU Lesser
11
General Public License as published by the Free Software Foundation; either
12
version 2.1 of the License, or (at your option) any later version. This
13
module is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16
details. You should have received a copy of the GNU Lesser General Public
17
License along with this module; if not, write to the Free Software Foundation,
18
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
19
20
#include "blargg_source.h"
21
22
static long const clock_rate = 3579545;
23
static int const osc_count = Ay_Apu::osc_count + Scc_Apu::osc_count;
24
25
using std::min;
26
using std::max;
27
28
Kss_Emu::Kss_Emu()
29
14
{
30
14
  sn = 0;
31
14
  set_type( gme_kss_type );
32
14
  set_silence_lookahead( 6 );
33
14
  static const char* const names [osc_count] = {
34
14
    "Square 1", "Square 2", "Square 3",
35
14
    "Wave 1", "Wave 2", "Wave 3", "Wave 4", "Wave 5"
36
14
  };
37
14
  set_voice_names( names );
38
39
14
  static int const types [osc_count] = {
40
14
    wave_type | 0, wave_type | 1, wave_type | 2,
41
14
    wave_type | 3, wave_type | 4, wave_type | 5, wave_type | 6, wave_type | 7
42
14
  };
43
14
  set_voice_types( types );
44
45
14
  memset( unmapped_read, 0xFF, sizeof unmapped_read );
46
14
}
47
48
14
Kss_Emu::~Kss_Emu() { unload(); }
49
50
void Kss_Emu::unload()
51
30
{
52
30
  delete sn;
53
30
  sn = 0;
54
30
  Classic_Emu::unload();
55
30
}
56
57
// Track info
58
59
static void copy_kss_fields( Kss_Emu::header_t const& h, track_info_t* out )
60
3.07k
{
61
3.07k
  const char* system = "MSX";
62
3.07k
  if ( h.device_flags & 0x02 )
63
512
  {
64
512
    system = "Sega Master System";
65
512
    if ( h.device_flags & 0x04 )
66
256
      system = "Game Gear";
67
68
512
    if ( h.device_flags & 0x01 )
69
256
      system = "Sega Mega Drive";
70
512
  }
71
3.07k
  Gme_File::copy_field_( out->system, system );
72
3.07k
}
73
74
blargg_err_t Kss_Emu::track_info_( track_info_t* out, int ) const
75
3.07k
{
76
3.07k
  copy_kss_fields( header_, out );
77
3.07k
  return 0;
78
3.07k
}
79
80
static blargg_err_t check_kss_header( void const* header )
81
12
{
82
12
  if ( memcmp( header, "KSCC", 4 ) && memcmp( header, "KSSX", 4 ) )
83
0
    return gme_wrong_file_type;
84
12
  return 0;
85
12
}
86
87
struct Kss_File : Gme_Info_
88
{
89
  Kss_Emu::header_t header_;
90
91
0
  Kss_File() { set_type( gme_kss_type ); }
92
93
  blargg_err_t load_( Data_Reader& in )
94
0
  {
95
0
    blargg_err_t err = in.read( &header_, Kss_Emu::header_size );
96
0
    if ( err )
97
0
      return (err == in.eof_error ? gme_wrong_file_type : err);
98
0
    return check_kss_header( &header_ );
99
0
  }
100
101
  blargg_err_t track_info_( track_info_t* out, int ) const
102
0
  {
103
0
    copy_kss_fields( header_, out );
104
0
    return 0;
105
0
  }
106
};
107
108
14
static Music_Emu* new_kss_emu () { return BLARGG_NEW Kss_Emu ; }
109
0
static Music_Emu* new_kss_file() { return BLARGG_NEW Kss_File; }
110
111
static gme_type_t_ const gme_kss_type_ = { "MSX", 256, &new_kss_emu, &new_kss_file, "KSS", 0x03 };
112
extern gme_type_t const gme_kss_type = &gme_kss_type_;
113
114
115
// Setup
116
117
void Kss_Emu::update_gain()
118
3.07k
{
119
3.07k
  double g = gain() * 1.4;
120
3.07k
  if ( scc_accessed )
121
0
    g *= 1.5;
122
3.07k
  ay.volume( g );
123
3.07k
  scc.volume( g );
124
3.07k
  if ( sn )
125
512
    sn->volume( g );
126
3.07k
}
127
128
blargg_err_t Kss_Emu::load_( Data_Reader& in )
129
14
{
130
14
  memset( &header_, 0, sizeof header_ );
131
14
  blaarg_static_assert( offsetof (header_t,device_flags) == header_size - 1, "KSS Header layout incorrect!" );
132
14
  blaarg_static_assert( offsetof (ext_header_t,msx_audio_vol) == ext_header_size - 1, "KSS Extended Header layout incorrect!" );
133
14
  RETURN_ERR( rom.load( in, header_size, STATIC_CAST(header_t*,&header_), 0 ) );
134
135
12
  RETURN_ERR( check_kss_header( header_.tag ) );
136
137
12
  if ( header_.tag [3] == 'C' )
138
9
  {
139
9
    if ( header_.extra_header )
140
2
    {
141
2
      header_.extra_header = 0;
142
2
      set_warning( "Unknown data in header" );
143
2
    }
144
9
    if ( header_.device_flags & ~0x0F )
145
3
    {
146
3
      header_.device_flags &= 0x0F;
147
3
      set_warning( "Unknown data in header" );
148
3
    }
149
9
  }
150
3
  else
151
3
  {
152
3
    ext_header_t& ext = header_;
153
3
    memcpy( &ext, rom.begin(), min( (int) ext_header_size, (int) header_.extra_header ) );
154
3
    if ( header_.extra_header > 0x10 )
155
1
      set_warning( "Unknown data in header" );
156
3
  }
157
158
12
  if ( header_.device_flags & 0x09 )
159
3
    set_warning( "FM sound not supported" );
160
161
12
  scc_enabled = 0xC000;
162
12
  if ( header_.device_flags & 0x04 )
163
4
    scc_enabled = 0;
164
165
12
  if ( header_.device_flags & 0x02 && !sn )
166
2
    CHECK_ALLOC( sn = BLARGG_NEW( Sms_Apu ) );
167
168
12
  set_voice_count( osc_count );
169
170
12
  return setup_buffer( ::clock_rate );
171
12
}
172
173
void Kss_Emu::update_eq( blip_eq_t const& eq )
174
12
{
175
12
  ay.treble_eq( eq );
176
12
  scc.treble_eq( eq );
177
12
  if ( sn )
178
2
    sn->treble_eq( eq );
179
12
}
180
181
void Kss_Emu::set_voice( int i, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right )
182
96
{
183
96
  int i2 = i - ay.osc_count;
184
96
  if ( i2 >= 0 )
185
60
    scc.osc_output( i2, center );
186
36
  else
187
36
    ay.osc_output( i, center );
188
96
  if ( sn && i < sn->osc_count )
189
8
    sn->osc_output( i, center, left, right );
190
96
}
191
192
// Emulation
193
194
void Kss_Emu::set_tempo_( double t )
195
12
{
196
12
  blip_time_t period =
197
12
      (header_.device_flags & 0x40 ? ::clock_rate / 50 : ::clock_rate / 60);
198
12
  play_period = blip_time_t (period / t);
199
12
}
200
201
blargg_err_t Kss_Emu::start_track_( int track )
202
3.07k
{
203
3.07k
  RETURN_ERR( Classic_Emu::start_track_( track ) );
204
205
3.07k
  memset( ram, 0xC9, 0x4000 );
206
3.07k
  memset( ram + 0x4000, 0, sizeof ram - 0x4000 );
207
208
  // copy driver code to lo RAM
209
3.07k
  static byte const bios [] = {
210
3.07k
    0xD3, 0xA0, 0xF5, 0x7B, 0xD3, 0xA1, 0xF1, 0xC9, // $0001: WRTPSG
211
3.07k
    0xD3, 0xA0, 0xDB, 0xA2, 0xC9                    // $0009: RDPSG
212
3.07k
  };
213
3.07k
  static byte const vectors [] = {
214
3.07k
    0xC3, 0x01, 0x00,   // $0093: WRTPSG vector
215
3.07k
    0xC3, 0x09, 0x00,   // $0096: RDPSG vector
216
3.07k
  };
217
3.07k
  memcpy( ram + 0x01, bios,    sizeof bios );
218
3.07k
  memcpy( ram + 0x93, vectors, sizeof vectors );
219
220
  // copy non-banked data into RAM
221
3.07k
  unsigned load_addr = get_le16( header_.load_addr );
222
3.07k
  long orig_load_size = get_le16( header_.load_size );
223
3.07k
  long load_size = min( orig_load_size, rom.file_size() );
224
3.07k
  load_size = min( load_size, long (mem_size - load_addr) );
225
3.07k
  if ( load_size != orig_load_size )
226
1.79k
    set_warning( "Excessive data size" );
227
3.07k
  memcpy( ram + load_addr, rom.begin() + header_.extra_header, load_size );
228
229
3.07k
  rom.set_addr( -load_size - header_.extra_header );
230
231
  // check available bank data
232
3.07k
  int32_t const bank_size = this->bank_size();
233
3.07k
  int max_banks = (rom.file_size() - load_size + bank_size - 1) / bank_size;
234
3.07k
  bank_count = header_.bank_mode & 0x7F;
235
3.07k
  if ( bank_count > max_banks )
236
512
  {
237
512
    bank_count = max_banks;
238
512
    set_warning( "Bank data missing" );
239
512
  }
240
  //debug_printf( "load_size : $%X\n", load_size );
241
  //debug_printf( "bank_size : $%X\n", bank_size );
242
  //debug_printf( "bank_count: %d (%d claimed)\n", bank_count, header_.bank_mode & 0x7F );
243
244
3.07k
  ram [idle_addr] = 0xFF;
245
3.07k
  cpu::reset( unmapped_write, unmapped_read );
246
3.07k
  cpu::map_mem( 0, mem_size, ram, ram );
247
248
3.07k
  ay.reset();
249
3.07k
  scc.reset();
250
3.07k
  if ( sn )
251
512
    sn->reset();
252
3.07k
  r.sp = 0xF380;
253
3.07k
  ram [--r.sp] = idle_addr >> 8;
254
3.07k
  ram [--r.sp] = idle_addr & 0xFF;
255
3.07k
  r.b.a = track;
256
3.07k
  r.pc = get_le16( header_.init_addr );
257
3.07k
  next_play = play_period;
258
3.07k
  scc_accessed = false;
259
3.07k
  gain_updated = false;
260
3.07k
  update_gain();
261
3.07k
  ay_latch = 0;
262
263
3.07k
  return 0;
264
3.07k
}
265
266
void Kss_Emu::set_bank( int logical, int physical )
267
12
{
268
12
  unsigned const bank_size = this->bank_size();
269
270
12
  unsigned addr = 0x8000;
271
12
  if ( logical && bank_size == 8 * 1024 )
272
0
    addr = 0xA000;
273
274
12
  physical -= header_.first_bank;
275
12
  if ( (unsigned) physical >= (unsigned) bank_count )
276
12
  {
277
12
    byte* data = ram + addr;
278
12
    cpu::map_mem( addr, bank_size, data, data );
279
12
  }
280
0
  else
281
0
  {
282
0
    long phys = physical * (int32_t) bank_size;
283
0
    for ( unsigned offset = 0; offset < bank_size; offset += page_size )
284
0
      cpu::map_mem( addr + offset, page_size,
285
0
          unmapped_write, rom.at_addr( phys + offset ) );
286
0
  }
287
12
}
288
289
void Kss_Emu::cpu_write( unsigned addr, int data )
290
389
{
291
389
  data &= 0xFF;
292
389
  switch ( addr )
293
389
  {
294
6
  case 0x9000:
295
6
    set_bank( 0, data );
296
6
    return;
297
298
6
  case 0xB000:
299
6
    set_bank( 1, data );
300
6
    return;
301
389
  }
302
303
377
  int scc_addr = (addr & 0xDFFF) ^ 0x9800;
304
377
  if ( scc_addr < scc.reg_count )
305
12
  {
306
12
    scc_accessed = true;
307
12
    scc.write( time(), scc_addr, data );
308
12
    return;
309
12
  }
310
311
365
  debug_printf( "LD ($%04X),$%02X\n", addr, data );
312
365
}
313
314
void kss_cpu_write( Kss_Cpu* cpu, unsigned addr, int data )
315
734k
{
316
734k
  *cpu->write( addr ) = data;
317
734k
  if ( (addr & STATIC_CAST(Kss_Emu&,*cpu).scc_enabled) == 0x8000 )
318
389
    STATIC_CAST(Kss_Emu&,*cpu).cpu_write( addr, data );
319
734k
}
320
321
void kss_cpu_out( Kss_Cpu* cpu, cpu_time_t time, unsigned addr, int data )
322
483
{
323
483
  data &= 0xFF;
324
483
  Kss_Emu& emu = STATIC_CAST(Kss_Emu&,*cpu);
325
483
  switch ( addr & 0xFF )
326
483
  {
327
0
  case 0xA0:
328
0
    emu.ay_latch = data & 0x0F;
329
0
    return;
330
331
0
  case 0xA1:
332
0
    GME_APU_HOOK( &emu, emu.ay_latch, data );
333
0
    emu.ay.write( time, emu.ay_latch, data );
334
0
    return;
335
336
0
  case 0x06:
337
0
    if ( emu.sn && (emu.header_.device_flags & 0x04) )
338
0
    {
339
0
      emu.sn->write_ggstereo( time, data );
340
0
      return;
341
0
    }
342
0
    break;
343
344
0
  case 0x7E:
345
0
  case 0x7F:
346
0
    if ( emu.sn )
347
0
    {
348
0
      GME_APU_HOOK( &emu, 16, data );
349
0
      emu.sn->write_data( time, data );
350
0
      return;
351
0
    }
352
0
    break;
353
354
0
  case 0xFE:
355
0
    emu.set_bank( 0, data );
356
0
    return;
357
358
  #ifndef NDEBUG
359
  case 0xF1: // FM data
360
    if ( data )
361
      break; // trap non-zero data
362
  case 0xF0: // FM addr
363
  case 0xA8: // PPI
364
    return;
365
  #endif
366
483
  }
367
368
483
  debug_printf( "OUT $%04X,$%02X\n", addr, data );
369
483
}
370
371
int kss_cpu_in( Kss_Cpu*, cpu_time_t, unsigned addr )
372
1
{
373
  //Kss_Emu& emu = STATIC_CAST(Kss_Emu&,*cpu);
374
  //switch ( addr & 0xFF )
375
  //{
376
  //}
377
378
1
  debug_printf( "IN $%04X\n", addr );
379
1
  return 0;
380
1
}
381
382
// Emulation
383
384
blargg_err_t Kss_Emu::run_clocks( blip_time_t& duration, int )
385
494k
{
386
2.00M
  while ( time() < duration )
387
1.50M
  {
388
1.50M
    blip_time_t end = min( duration, next_play );
389
1.50M
    cpu::run( min( duration, next_play ) );
390
1.50M
    if ( r.pc == idle_addr )
391
1.49M
      set_time( end );
392
393
1.50M
    if ( time() >= next_play )
394
1.46M
    {
395
1.46M
      next_play += play_period;
396
1.46M
      if ( r.pc == idle_addr )
397
1.45M
      {
398
1.45M
        if ( !gain_updated )
399
3.07k
        {
400
3.07k
          gain_updated = true;
401
3.07k
          if ( scc_accessed )
402
0
            update_gain();
403
3.07k
        }
404
405
1.45M
        ram [--r.sp] = idle_addr >> 8;
406
1.45M
        ram [--r.sp] = idle_addr & 0xFF;
407
1.45M
        r.pc = get_le16( header_.play_addr );
408
1.45M
        GME_FRAME_HOOK( this );
409
1.45M
      }
410
1.46M
    }
411
1.50M
  }
412
413
494k
  duration = time();
414
494k
  next_play -= duration;
415
494k
  check( next_play >= 0 );
416
494k
  adjust_time( -duration );
417
494k
  ay.end_frame( duration );
418
494k
  scc.end_frame( duration );
419
494k
  if ( sn )
420
82.4k
    sn->end_frame( duration );
421
422
494k
  return 0;
423
494k
}