* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / link / z80 / lkgg.c
1 /* lkgg.c
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 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
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20  * P. Felber
21  */
22
23 #ifdef GAMEGEAR
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "aslink.h"
30
31 #define CARTSIZE ((unsigned long)4*16UL*1024UL)
32 #define NBSEG 8UL
33 #define SEGSIZE (CARTSIZE/NBSEG)
34
35 unsigned char *cart[NBSEG];
36
37 #define ROMSIZE 0x10000UL
38 #define BANKSIZE 0x4000UL
39
40 int current_rom_bank;
41
42 VOID gg(int in)
43 {
44   static int first = 1;
45   unsigned long pos;
46   int i;
47
48   if(first) {
49     for(i = 0; i < NBSEG; i++) {
50       if((cart[i] = malloc(SEGSIZE)) == NULL) {
51         fprintf(stderr, "ERROR: can't allocate %dth segment of memory (%d bytes)\n", i, (int)SEGSIZE);
52         exit(EXIT_FAILURE);
53       }
54       memset(cart[i], 0, SEGSIZE);
55     }
56     first = 0;
57   }
58   if(in) {
59     if(rtcnt > 2) {
60       if(hilo == 0)
61         pos = rtval[0] | (rtval[1]<<8);
62       else
63         pos = rtval[1] | (rtval[0]<<8);
64
65       /* Perform some validity checks */
66       if(pos >= ROMSIZE) {
67         fprintf(stderr, "ERROR: address overflow (addr %lx >= %lx)\n", pos, ROMSIZE);
68         exit(EXIT_FAILURE);
69       }
70       if(current_rom_bank > 1)
71         pos += (current_rom_bank-1)*BANKSIZE;
72       for(i = 2; i < rtcnt; i++) {
73         if(rtflg[i]) {
74           if(pos < CARTSIZE) {
75             if(cart[pos/SEGSIZE][pos%SEGSIZE] != 0)
76               fprintf(stderr, "WARNING: wrote twice at addr %lx (%02X->%02X)\n", pos, rtval[i], cart[pos/SEGSIZE][pos%SEGSIZE]);
77             cart[pos/SEGSIZE][pos%SEGSIZE] = rtval[i];
78           } else {
79             fprintf(stderr, "ERROR: cartridge size overflow (addr %lx >= %lx)\n", pos, CARTSIZE);
80             exit(EXIT_FAILURE);
81           }
82           pos++;
83         }
84       }
85     }
86   } else {
87     /* EOF */
88     /* Patch before calculating the checksum */
89     for(i = 0; i < NBSEG; i++)
90       fwrite(cart[i], 1, SEGSIZE, ofp);
91   }
92 }
93
94 #endif /* GAMEGEAR */