File: usource.c

package info (click to toggle)
fuse-emulator 1.3.2%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,456 kB
  • ctags: 9,261
  • sloc: ansic: 83,215; sh: 11,503; perl: 3,806; makefile: 1,000; yacc: 254; lex: 143
file content (248 lines) | stat: -rw-r--r-- 6,545 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* usource.c: Routines for handling the Currah uSource interface
   Copyright (c) 2007-2016 Stuart Brady, Philip Kendall
   Copyright (c) 2016 Fredrick Meunier

   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:

   Philip: philip-fuse@shadowmagic.org.uk

   Stuart: stuart.brady@gmail.com

*/

#include <config.h>

#include <string.h>

#include <libspectrum.h>

#include "compat.h"
#include "infrastructure/startup_manager.h"
#include "machine.h"
#include "memory.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "unittests/unittests.h"
#include "usource.h"

/* An 8 KiB memory chunk accessible by the Z80 when /ROMCS is low
 * (mirrored in the second 8 KiB when active) */
static memory_page usource_memory_map_romcs[ MEMORY_PAGES_IN_8K ];
static int usource_memory_source;

int usource_active = 0;
int usource_available = 0;

static void usource_toggle_write( libspectrum_word port,
				  libspectrum_byte val );
static libspectrum_byte usource_toggle_read( libspectrum_word port,
					     libspectrum_byte *attached );

static void usource_reset( int hard_reset );
static void usource_enabled_snapshot( libspectrum_snap *snap );
static void usource_from_snapshot( libspectrum_snap *snap );
static void usource_to_snapshot( libspectrum_snap *snap );
static void usource_memory_map( void );

static module_info_t usource_module_info = {

  /* .reset = */ usource_reset,
  /* .romcs = */ usource_memory_map,
  /* .snapshot_enabled = */ usource_enabled_snapshot,
  /* .snapshot_from = */ usource_from_snapshot,
  /* .snapshot_to = */ usource_to_snapshot,

};

static const periph_port_t usource_ports[] = {
  { 0xffff, 0x2bae, usource_toggle_read, usource_toggle_write },

  { 0, 0, NULL, NULL }
};

static const periph_t usource_periph = {
  /* .option = */ &settings_current.usource,
  /* .ports = */ usource_ports,
  /* .hard_reset = */ 1,
  /* .activate = */ NULL,
};

static int
usource_init( void *context )
{
  int i;

  module_register( &usource_module_info );

  usource_memory_source = memory_source_register( "uSource" );
  for( i = 0; i < MEMORY_PAGES_IN_8K; i++ )
    usource_memory_map_romcs[i].source = usource_memory_source;

  periph_register( PERIPH_TYPE_USOURCE, &usource_periph );

  return 0;
}

static void
usource_end( void )
{
  usource_available = 0;
}

void
usource_register_startup( void )
{
  startup_manager_module dependencies[] = {
    STARTUP_MANAGER_MODULE_MEMORY,
    STARTUP_MANAGER_MODULE_SETUID,
  };
  startup_manager_register( STARTUP_MANAGER_MODULE_USOURCE, dependencies,
                            ARRAY_SIZE( dependencies ), usource_init, NULL,
                            usource_end );
}

static void
usource_reset( int hard_reset GCC_UNUSED )
{
  usource_active = 0;
  usource_available = 0;

  if( !periph_is_active( PERIPH_TYPE_USOURCE ) )
    return;

  if( machine_load_rom_bank( usource_memory_map_romcs, 0,
			     settings_current.rom_usource,
			     settings_default.rom_usource, 0x2000 ) ) {
    settings_current.usource = 0;
    periph_activate_type( PERIPH_TYPE_USOURCE, 0 );
    return;
  }

  machine_current->ram.romcs = 0;

  usource_available = 1;
}

void
usource_toggle( void )
{
  usource_active = !usource_active;
  machine_current->ram.romcs = usource_active;
  machine_current->memory_map();
}

static void
usource_memory_map( void )
{
  if( !usource_active ) return;

  memory_map_romcs_8k( 0x0000, usource_memory_map_romcs );
  memory_map_romcs_8k( 0x2000, usource_memory_map_romcs );
}

static libspectrum_byte
usource_toggle_read( libspectrum_word port GCC_UNUSED,
		     libspectrum_byte *attached GCC_UNUSED )
{
  usource_toggle();

  return 0xff;
}

static void
usource_toggle_write( libspectrum_word port GCC_UNUSED, libspectrum_byte val )
{
  usource_toggle();
}

int
usource_unittest( void )
{
  int r = 0;

  usource_active = 1;
  usource_memory_map();

  r += unittests_assert_8k_page( 0x0000, usource_memory_source, 0 );
  r += unittests_assert_8k_page( 0x2000, usource_memory_source, 0 );
  r += unittests_assert_16k_ram_page( 0x4000, 5 );
  r += unittests_assert_16k_ram_page( 0x8000, 2 );
  r += unittests_assert_16k_ram_page( 0xc000, 0 );

  usource_active = 0;
  machine_current->memory_map();

  r += unittests_paging_test_48( 2 );

  return r;
}

static void
usource_enabled_snapshot( libspectrum_snap *snap )
{
  if( libspectrum_snap_usource_active( snap ) )
    settings_current.usource = 1;
}

static void
usource_from_snapshot( libspectrum_snap *snap )
{
  if( !libspectrum_snap_usource_active( snap ) ) return;

  if( libspectrum_snap_usource_custom_rom( snap ) &&
      libspectrum_snap_usource_rom( snap, 0 ) &&
      machine_load_rom_bank_from_buffer(
                             usource_memory_map_romcs, 0,
                             libspectrum_snap_usource_rom( snap, 0 ),
                             libspectrum_snap_usource_rom_length( snap, 0 ),
                             1 ) )
    return;

  if( libspectrum_snap_usource_paged( snap ) ) {
    usource_active = 0; /* Will be toggled to active next */
    usource_toggle();
  }
}

static void
usource_to_snapshot( libspectrum_snap *snap )
{
  libspectrum_byte *buffer;
  size_t rom_length;
  int i;

  if( !periph_is_active( PERIPH_TYPE_USOURCE ) ) return;

  libspectrum_snap_set_usource_active( snap, 1 );
  libspectrum_snap_set_usource_paged ( snap, usource_active );

  if( usource_memory_map_romcs[0].save_to_snapshot ) {
    rom_length = 0x2000;

    libspectrum_snap_set_usource_custom_rom( snap, 1 );
    libspectrum_snap_set_usource_rom_length( snap, 0, rom_length );

    buffer = libspectrum_new( libspectrum_byte, rom_length );

    for( i = 0; i < MEMORY_PAGES_IN_8K; i++ )
      memcpy( buffer + i * MEMORY_PAGE_SIZE,
              usource_memory_map_romcs[ i ].page, MEMORY_PAGE_SIZE );

    libspectrum_snap_set_usource_rom( snap, 0, buffer );
  }
}