1 /* spec_se.c: ZX Spectrum SE specific routines
2    Copyright (c) 1999-2011 Fredrick Meunier, Philip Kendall, Darren Salt
3 
4    $Id: spec_se.c 4862 2013-01-27 11:00:36Z fredm $
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20    Author contact information:
21 
22    E-mail: philip-fuse@shadowmagic.org.uk
23 
24 */
25 
26 #include <config.h>
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include <libspectrum.h>
32 
33 #include "fuse.h"
34 #include "keyboard.h"
35 #include "machine.h"
36 #include "machines.h"
37 #include "machines_periph.h"
38 #include "memory.h"
39 #include "peripherals/dck.h"
40 #include "peripherals/scld.h"
41 #include "snapshot.h"
42 #include "sound.h"
43 #include "spec128.h"
44 #include "settings.h"
45 #include "spectrum.h"
46 #include "tc2068.h"
47 #include "ui/ui.h"
48 
49 static void dock_exrom_reset( void );
50 static int spec_se_reset( void );
51 static int spec_se_memory_map( void );
52 
53 int
spec_se_init(fuse_machine_info * machine)54 spec_se_init( fuse_machine_info *machine )
55 {
56   machine->machine = LIBSPECTRUM_MACHINE_SE;
57   machine->id = "se";
58 
59   machine->reset = spec_se_reset;
60 
61   machine->timex = 1;
62   machine->ram.port_from_ula = tc2048_port_from_ula;
63   machine->ram.contend_delay = spectrum_contend_delay_65432100;
64   machine->ram.contend_delay_no_mreq = spectrum_contend_delay_65432100;
65   machine->ram.valid_pages = 9;
66 
67   machine->unattached_port = spectrum_unattached_port_none;
68 
69   machine->shutdown = NULL;
70 
71   machine->memory_map = spec_se_memory_map;
72 
73   return 0;
74 }
75 
76 static void
dock_exrom_reset(void)77 dock_exrom_reset( void )
78 {
79   /* The dock is always active on the SE */
80   dck_active = 1;
81 }
82 
83 int
spec_se_reset(void)84 spec_se_reset( void )
85 {
86   int error;
87   size_t i, j;
88 
89   dock_exrom_reset();
90 
91   error = machine_load_rom( 0, settings_current.rom_spec_se_0,
92                             settings_default.rom_spec_se_0, 0x4000 );
93   if( error ) return error;
94   error = machine_load_rom( 1, settings_current.rom_spec_se_1,
95                             settings_default.rom_spec_se_1, 0x4000 );
96   if( error ) return error;
97 
98   scld_home_map_16k( 0x0000, memory_map_rom, 0 );
99   scld_home_map_16k( 0x4000, memory_map_ram, 5 );
100   scld_home_map_16k( 0x8000, memory_map_ram, 8 );
101   scld_home_map_16k( 0xc000, memory_map_ram, 0 );
102 
103   /* RAM pages 1, 3, 5 and 7 contended */
104   for( i = 0; i < 8; i++ )
105     memory_ram_set_16k_contention( i, i & 1 );
106 
107   periph_clear();
108   machines_periph_128();
109 
110   /* SE style memory paging present */
111   periph_set_present( PERIPH_TYPE_128_MEMORY, PERIPH_PRESENT_NEVER );
112   periph_set_present( PERIPH_TYPE_SE_MEMORY, PERIPH_PRESENT_ALWAYS );
113 
114   /* ULA uses full decoding */
115   periph_set_present( PERIPH_TYPE_ULA, PERIPH_PRESENT_NEVER );
116   periph_set_present( PERIPH_TYPE_ULA_FULL_DECODE, PERIPH_PRESENT_ALWAYS );
117 
118   /* As does the AY chip */
119   periph_set_present( PERIPH_TYPE_AY, PERIPH_PRESENT_NEVER );
120   periph_set_present( PERIPH_TYPE_AY_FULL_DECODE, PERIPH_PRESENT_ALWAYS );
121 
122   /* Timex-style AY also present */
123   periph_set_present( PERIPH_TYPE_AY_TIMEX, PERIPH_PRESENT_ALWAYS );
124 
125   /* SCLD always present */
126   periph_set_present( PERIPH_TYPE_SCLD, PERIPH_PRESENT_ALWAYS );
127 
128   /* ZX Printer available */
129   periph_set_present( PERIPH_TYPE_ZXPRINTER_FULL_DECODE,
130                       PERIPH_PRESENT_OPTIONAL );
131 
132   for( i = 0; i < 8; i++ ) {
133 
134     libspectrum_byte *dock_ram = memory_pool_allocate( 0x2000 );
135     libspectrum_byte *exrom_ram = memory_pool_allocate( 0x2000 );
136 
137     for( j = 0; j < MEMORY_PAGES_IN_8K; j++ ) {
138 
139       int page_num = i * MEMORY_PAGES_IN_8K + j;
140 
141       timex_dock[page_num].page = dock_ram + j * MEMORY_PAGE_SIZE;
142       timex_dock[page_num].offset = j * MEMORY_PAGE_SIZE;
143       timex_dock[page_num].page_num = i;
144       timex_dock[page_num].contended = 0;
145       timex_dock[page_num].writable = 1;
146       timex_dock[page_num].save_to_snapshot = 1;
147       timex_dock[page_num].source = memory_source_dock;
148 
149       timex_exrom[page_num].page = exrom_ram + j * MEMORY_PAGE_SIZE;
150       timex_exrom[page_num].offset = j * MEMORY_PAGE_SIZE;
151       timex_exrom[page_num].page_num = i;
152       timex_exrom[page_num].contended = 0;
153       timex_exrom[page_num].writable = 1;
154       timex_exrom[page_num].save_to_snapshot = 1;
155       timex_exrom[page_num].source = memory_source_exrom;
156     }
157   }
158 
159   /* The dock and exrom aren't cleared by the reset routine, so do
160      so manually (only really necessary to keep snapshot sizes down) */
161   for( i = 0; i < MEMORY_PAGES_IN_64K; i++ ) {
162     memset( timex_dock[i].page,  0, MEMORY_PAGE_SIZE );
163     memset( timex_exrom[i].page, 0, MEMORY_PAGE_SIZE );
164   }
165 
166   machine_current->ram.locked = 0;
167   machine_current->ram.last_byte = 0;
168 
169   machine_current->ram.current_page=0;
170   machine_current->ram.current_rom=0;
171 
172   memory_current_screen = 5;
173   memory_screen_mask = 0xdfff;
174 
175   /* Make sure SCLD and friends are enabled, calls memory_map() as a side
176      effect so we need memory related variables etc. to be initialised */
177   periph_update();
178 
179   scld_dec_write( 0x00ff, 0x80 );
180   scld_dec_write( 0x00ff, 0x00 );
181   scld_hsr_write( 0x00f4, 0x00 );
182 
183   tc2068_tc2048_common_display_setup();
184 
185   return 0;
186 }
187 
188 static int
spec_se_memory_map(void)189 spec_se_memory_map( void )
190 {
191   memory_page *exrom_dock;
192 
193   scld_memory_map_home();
194 
195   /* Spectrum SE memory paging is just a combination of the 128K
196      0x7ffd and Timex DOCK/EXROM paging schemes with one exception */
197   spec128_memory_map();
198   scld_memory_map();
199 
200   /* Exceptions apply if an odd bank is paged in via 0x7ffd */
201   if( machine_current->ram.current_page & 0x01 ) {
202 
203   /* If so, bits 2 and 3 of 0xf4 also control whether the DOCK/EXROM
204      is paged in at 0xc000 and 0xe000 respectively */
205     exrom_dock =
206       scld_last_dec.name.altmembank ? timex_exrom : timex_dock;
207 
208     if( scld_last_hsr & ( 1 << 2 ) )
209       memory_map_8k( 0xc000, exrom_dock, 6 );
210 
211     if( scld_last_hsr & ( 1 << 3 ) )
212       memory_map_8k( 0xe000, exrom_dock, 7 );
213   }
214 
215   memory_romcs_map();
216 
217   return 0;
218 }
219