aboutsummaryrefslogtreecommitdiff
path: root/src/nesout.c
blob: 44ab1afca3fb5647c00232ea046f7c709eb87bae (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*

    casm - Simple, portable assembler

    Copyright (C) 2003-2015  Ian Cowburn (ianc@noddybox.co.uk)

    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 3 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, see <http://www.gnu.org/licenses/>.

    -------------------------------------------------------------------------

    NES ROM output handler.

    Notes:

        * ROM is 0x8000 to 0xffff (just 0xc000 to 0xffff if 16K ROM)

        * VROM is at 0x0000 to 0x1fff.

*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "global.h"
#include "expr.h"
#include "codepage.h"
#include "nesout.h"


/* ---------------------------------------- MACROS & TYPES
*/

enum option_t
{
    OPT_VECTOR,
    OPT_TV_FORMAT,
    OPT_MAPPER
};

static const ValueTable option_set[] =
{
    {"nes-vector",              OPT_VECTOR},
    {"nes-tv-format",           OPT_TV_FORMAT},
    {"nes-mapper",              OPT_MAPPER},
    {NULL}
};

typedef enum
{
    VECTOR_RESET,
    VECTOR_NMI,
    VECTOR_BRK
} VectorType;

static ValueTable       vector_table[] =
{
    {"reset",           VECTOR_RESET},
    {"nmi",             VECTOR_NMI},
    {"brk",             VECTOR_BRK},
    {NULL}
};

typedef enum
{
    PAL = 1,
    NTSC = 0
} TVFormat;

static ValueTable       format_table[] =
{
    {"pal",             PAL},
    {"NTSC",            NTSC},
    {NULL}
};


static struct
{
    int         vector[3];
    TVFormat    tv_format;
    int         mapper;
} option =
{
    {-1, -1, -1}, PAL, 0
};


/* ---------------------------------------- PRIVATE FUNCTIONS
*/

static int PokeB(Byte *mem, int addr, Byte b)
{
    mem[addr++] = b;
    return (addr % 0x10000);
}


static int PokeW(Byte *mem, int addr, int w)
{
    addr = PokeB(mem, addr, w & 0xff);
    return PokeB(mem, addr, (w & 0xff00) >> 8);
}


static int PokeS(Byte *mem, int addr, const char *str, int maxlen, char pad)
{
    while(*str && maxlen--)
    {
        addr = PokeB(mem, addr, CodeFromNative(CP_ASCII, *str++));
    }

    while(maxlen-- > 0)
    {
        addr = PokeB(mem, addr, CodeFromNative(CP_ASCII, pad));
    }

    return addr;
}

static unsigned CalcChecksum(Byte *p, int len, unsigned csum)
{
    while(len-- > 0)
    {
        csum += *p++;
    }

    return csum & 0xffff;
}


/* ---------------------------------------- INTERFACES
*/
const ValueTable *NESOutputOptions(void)
{
    return option_set;
}

CommandStatus NESOutputSetOption(int opt, int argc, char *argv[],
                                  int quoted[], char *err, size_t errsize)
{
    const ValueTable *val;
    int f;

    CMD_ARGC_CHECK(1);

    switch(opt)
    {
        case OPT_VECTOR:
            CMD_ARGC_CHECK(2);
            CMD_TABLE(argv[0], vector_table, val);
            CMD_EXPR(argv[1], f);
            option.vector[val->value] = f;
            break;

        case OPT_TV_FORMAT:
            CMD_TABLE(argv[0], format_table, val);
            option.tv_format = val->value;
            break;

        case OPT_MAPPER:
            CMD_EXPR(argv[1], f);
            option.mapper = f;
            break;

        default:
            break;
    }

    return CMD_OK;

}

int NESOutput(const char *filename, const char *filename_bank,
              MemoryBank **bank, int count, char *error, size_t error_size)
{
    FILE *fp;
    Byte *mem;
    int num_rom;
    int num_vrom;
    int is_16k;
    int first_code;
    int start;
    int f;

    /* Go through the banks and see which are code (ROM), and which char (VROM)
    */
    num_rom = 0;
    num_vrom = 0;
    is_16k = FALSE;
    first_code = -1;

    for(f = 0; f < count; f++)
    {
        if (bank[f]->max_address_used < 0x2000)
        {
            num_vrom++;
        }
        else if (bank[f]->min_address_used >= 0xc000)
        {
            if (first_code == -1)
            {
                first_code = f;
            }

            is_16k = TRUE;
            num_rom++;
        }
        else if (bank[f]->min_address_used >= 0x8000)
        {
            if (first_code == -1)
            {
                first_code = f;
            }

            is_16k = FALSE;
            num_rom++;
        }
        else
        {
            snprintf(error, error_size,
                        "Banks should use memory in the range 0x0000 - 0x1fff "
                        "to indicate they are\nvideo ROM, 0x8000 - 0xffff to "
                        "indicate they are program ROM or 0xc000 - 0xffff\n"
                        "to indicate a single 16K ROM segment.");

            return FALSE;
        }
    }

    if (first_code == -1)
    {
        snprintf(error, error_size,
                    "No ROM code banks present;\n"
                    "Banks should use memory in the range 0x0000 - 0x1fff "
                    "to indicate they are\nvideo ROM, 0x8000 - 0xffff to "
                    "indicate they are program ROM or 0xc000 - 0xffff\n"
                    "to indicate a single 16K ROM segment.");

        return FALSE;
    }

    if (!(fp = fopen(filename, "wb")))
    {
        snprintf(error, error_size, "Failed to create %s", filename);
        return FALSE;
    }

    /* Setup vectors
    */
    mem = bank[first_code]->memory;

    start = option.vector[VECTOR_RESET];

    if (start == -1)
    {
        start = is_16k ? 0xc000 : 0x8000;

        fprintf(stderr, "WARNING: No reset vector provided; assuming 0x%4.4x\n",
                                        start);
    }

    PokeW(mem, 0xfffc, start);

    if (option.vector[VECTOR_NMI] != -1)
    {
        PokeW(mem, 0xfffa, option.vector[VECTOR_NMI]);
    }
    else
    {
        fprintf(stderr, "WARNING: NMI vector not set\n");
    }

    if (option.vector[VECTOR_BRK] != -1)
    {
        PokeW(mem, 0xfffe, option.vector[VECTOR_BRK]);
    }

    /* Output header
    */
    fputs("NES", fp);
    fputc(26, fp);

    if (is_16k)
    {
        fputc(1, fp);
    }
    else
    {
        fputc(num_rom * 2, fp);
    }

    fputc(num_vrom, fp);

    fputc((option.mapper & 0x0f) << 4, fp);
    fputc(option.mapper & 0xf0, fp);

    fputc(0, fp);

    fputc(option.tv_format, fp);

    for(f = 0; f < 6; f++)
    {
        fputc(0, fp);
    }

    /* Output ROM contents, first code segments
    */
    for(f = 0; f < count; f++)
    {
        if (bank[f]->min_address_used > 0x2000)
        {
            if (is_16k)
            {
                fwrite(bank[f]->memory + 0xc000, 0x4000, 1, fp);
            }
            else
            {
                fwrite(bank[f]->memory + 0x8000, 0x8000, 1, fp);
            }
        }
    }

    /* Then video ROM banks
    */
    for(f = 0; f < count; f++)
    {
        if (bank[f]->min_address_used < 0x2000)
        {
            fwrite(bank[f]->memory, 0x2000, 1, fp);
        }
    }

    fclose(fp);

    return TRUE;
}


/*
vim: ai sw=4 ts=8 expandtab
*/