@@ -35,6 +35,9 @@ Place, Suite 330, Boston, MA 02111-1307 USA
3535#define H_KEYI 0xFD9A
3636#define H_TIMI 0xFD9F
3737
38+ #define EXTBIO 0xFFCA
39+ #define CALL_HL 70001$
40+
3841// Warning: execution fails when the buffers are put inside main.
3942// In main they're in the stack space, and here it's global.
4043char buffer [1024 ];
@@ -64,7 +67,8 @@ unsigned char VDP_regs[8];
6467unsigned char slots ;
6568unsigned char slot_p0 , slot_p1 ;
6669
67- int segment ;
70+ unsigned char segment ;
71+ unsigned char segments [4 ];
6872int fH ;
6973unsigned int i , j ;
7074
@@ -190,28 +194,90 @@ void main(char *argv[], int argc) {
190194 // Don't attempt to put the ROM - DEBUG
191195 //rom_selected_p0 = 0;
192196 //rom_selected_p1 = 0;
197+
198+ // Allocate segments
199+ /* Parameter: A = 0
200+ D = 4 (device number of mapper support)
201+ E = 1
202+ Result: A = slot address of primary mapper
203+ DE = reserved
204+ HL = start address of mapper variable table
205+ */
206+ __asm
207+ push af
208+ push de
209+ push hl
210+ push iy
211+
212+ xor a // A=0
213+ ld de , #0x0402 // D=4, E=2
214+ call EXTBIO
215+ // Now we have in HL the address of the mapper call table
216+
217+ xor a
218+ ld b , a // A=0, B=0
219+ call CALL_HL // ALL_SEG
220+
221+ ld iy , #_segments
222+ ld 0 (iy ), a
223+ //
224+ xor a
225+ ld b , a // A=0, B=0
226+ call CALL_HL // ALL_SEG
227+
228+ ld iy , #_segments
229+ ld 1 (iy ), a
230+ //
231+ xor a
232+ ld b , a // A=0, B=0
233+ call CALL_HL // ALL_SEG
234+
235+ ld iy , #_segments
236+ ld 2 (iy ), a
237+ //
238+ xor a
239+ ld b , a // A=0, B=0
240+ call CALL_HL // ALL_SEG
241+
242+ ld iy , #_segments
243+ ld 3 (iy ), a
244+
245+ pop iy
246+ pop hl
247+ pop de
248+ pop af
249+ jp 70002 $ // Get out
250+
251+ CALL_HL :
252+ jp (hl )
253+ 70002 $ :
254+ __endasm ;
193255
194256 // Read RAM
195- for (segment = 10 ; segment < 14 ; segment ++ ) {
257+ for (i = 0 ; i < 4 ; i ++ ) {
258+ segment = segments [i ];
196259 printf ("Filling segment %d\r" , segment );
197260
198261 to = (unsigned char * )0x8000 ;
199262
200- for (i = 0 ; i < 16 * 1024 / sizeof (buffer ); i ++ ) {
263+ for (j = 0 ; j < 16 * 1024 / sizeof (buffer ); j ++ ) {
201264 Read (fH , buffer , sizeof (buffer ));
202265
203266 // Don't overwritte MSX-DOS variables area
204- if (rom_selected_p0 && regs .im != 2 && segment == 13 && i >= 14 )
205- from = (unsigned char * )(0xC000 + i * sizeof (buffer ));
267+ if (rom_selected_p0 && regs .im != 2 && segment == segments [ 3 ] && j >= 14 )
268+ from = (unsigned char * )(0xC000 + j * sizeof (buffer ));
206269 else
207270 from = buffer ; // If IM = 2 actually we don't care about overwritting
208271
272+ #ifdef DEBUG
273+ printf ("Copy to " ); PrintHex ((unsigned int )to ); printf ("\r\n" );
274+ #endif
209275 OutPort (0xFE , segment ); // FE (write) Mapper segment for page 2 (#8000-#BFFF)
210276 MemCopy (to , from , sizeof (buffer ));
211277
212278 // If rom_selected_p0, we need to copy the
213279 // H.KEYI and H.TIMI hooks the game configured
214- if (rom_selected_p0 && segment == 13 && i == 15 ) {
280+ if (rom_selected_p0 && segment == segments [ 3 ] && j == 15 ) {
215281 MemCopy ((unsigned char * )(to + H_TIMI % sizeof (buffer )), (unsigned char * )(buffer + H_TIMI % sizeof (buffer )), 3 );
216282 MemCopy ((unsigned char * )(to + H_KEYI % sizeof (buffer )), (unsigned char * )(buffer + H_KEYI % sizeof (buffer )), 3 );
217283 }
@@ -231,7 +297,6 @@ void main(char *argv[], int argc) {
231297 #ifndef DEBUG
232298 unsigned char VRAM_Kb = GetVramSize ();
233299 FillVram (0 , 0 , VRAM_Kb * 1024 );
234- //SetBorderColor(1);
235300
236301 // Dump 64 Kb of VRAM
237302 for (i = 0 ; i < 16 * 1024 ; i += sizeof (buffer )) {
@@ -243,21 +308,22 @@ void main(char *argv[], int argc) {
243308
244309 Close (fH );
245310
246- // Put page 3 of the game (segment 13 ) in our page 3
247- // Put page 2 of the game (segment 12 ) in our page 2
248- // Put page 1 of the game (segment 11 ) in our page 1
311+ // Put page 3 of the game (segments[3]] ) in our page 3
312+ // Put page 2 of the game (segments[2] ) in our page 2
313+ // Put page 1 of the game (segments[1] ) in our page 1
249314 // Page 0 not yet, since it's where we're executing now!
250315 #ifndef DEBUG
251316 __asm
252317 di
253-
254- ld a , #13
318+
319+ ld iy , #_segments
320+ ld a , 3 (iy )
255321 out (0xFF ), a
256322
257- ld a , #12
323+ ld a , 2 ( iy )
258324 out (0xFE ), a
259325
260- ld a , #11
326+ ld a , 1 ( iy )
261327 out (0xFD ), a
262328 __endasm ;
263329 #endif
@@ -335,9 +401,9 @@ void main(char *argv[], int argc) {
335401 * ptr ++ = 0xFD ; // OUT (0xFD), A
336402 }
337403 } else {
338- // Set segment 10 for page 0
404+ // Set segments[0] for page 0
339405 * ptr ++ = 0x3E ;
340- * ptr ++ = 10 ; // LD A, 10
406+ * ptr ++ = segments [ 0 ] ; // LD A, START_SEGMENT
341407 //
342408 * ptr ++ = 0xD3 ;
343409 * ptr ++ = 0xFC ; // OUT (0xFC), A
0 commit comments