restored else branch for other ports
[fw/sdcc] / link / z80 / lkgg.c
1 /* lkgg.c */
2
3 /*
4  * P. Felber
5  */
6
7 #ifdef GAMEGEAR
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include "aslink.h"
15
16 #define CARTSIZE ((unsigned long)4*16UL*1024UL)
17 #define NBSEG 8UL
18 #define SEGSIZE (CARTSIZE/NBSEG)
19
20 unsigned char *cart[NBSEG];
21
22 #define ROMSIZE 0x10000UL
23 #define BANKSIZE 0x4000UL
24
25 int current_rom_bank;
26
27 VOID gg(int in)
28 {
29   static int first = 1;
30   unsigned long pos;
31   int i;
32
33   if(first) {
34     for(i = 0; i < NBSEG; i++) {
35       if((cart[i] = malloc(SEGSIZE)) == NULL) {
36         fprintf(stderr, "ERROR: can't allocate %dth segment of memory (%d bytes)\n", i, (int)SEGSIZE);
37         exit(-1);
38       }
39       memset(cart[i], 0, SEGSIZE);
40     }
41     first = 0;
42   }
43   if(in) {
44     if(rtcnt > 2) {
45       if(hilo == 0)
46         pos = rtval[0] | (rtval[1]<<8);
47       else
48         pos = rtval[1] | (rtval[0]<<8);
49
50       /* Perform some validity checks */
51       if(pos >= ROMSIZE) {
52         fprintf(stderr, "ERROR: address overflow (addr %lx >= %lx)\n", pos, ROMSIZE);
53         exit(-1);
54       }
55       if(current_rom_bank > 1)
56         pos += (current_rom_bank-1)*BANKSIZE;
57       for(i = 2; i < rtcnt; i++) {
58         if(rtflg[i]) {
59           if(pos < CARTSIZE) {
60             if(cart[pos/SEGSIZE][pos%SEGSIZE] != 0)
61               fprintf(stderr, "WARNING: wrote twice at addr %lx (%02X->%02X)\n", pos, rtval[i], cart[pos/SEGSIZE][pos%SEGSIZE]);
62             cart[pos/SEGSIZE][pos%SEGSIZE] = rtval[i];
63           } else {
64             fprintf(stderr, "ERROR: cartridge size overflow (addr %lx >= %lx)\n", pos, CARTSIZE);
65             exit(-1);
66           }
67           pos++;
68         }
69       }
70     }
71   } else {
72     /* EOF */
73     /* Patch before calculating the checksum */
74     for(i = 0; i < NBSEG; i++)
75       fwrite(cart[i], 1, SEGSIZE, ofp);
76   }
77 }
78
79 #endif /* GAMEGEAR */