/* ======================================================================== */
/*  Routines for writing a .BIN and .CFG from an icartrom_t.                */
/* ------------------------------------------------------------------------ */
/*  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 2 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, write to the Free Software             */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               */
/* ------------------------------------------------------------------------ */
/*                 Copyright (c) 1998-2001, Joseph Zbiciak                  */
/* ======================================================================== */
#ifndef ICARTBIN_H_
#define ICARTBIN_H_ 1
#include "icart/icartrom.h"

/* ======================================================================== */
/*  ROUTINES FOR WRITING BIN+CFG                                            */
/* ======================================================================== */

/* ======================================================================== */
/*  ICB_SHOW_RANGES                                                         */
/*  Shows a list of ranges of addresses represented by a bit-vector.        */
/* ======================================================================== */
void icb_show_ranges(uint_32 *bv);

/* ======================================================================== */
/*  ICB_WRITE_MAPPINGS                                                      */
/*  Writes the [mappings] section of a .CFG file, based on the icartrom.    */
/* ======================================================================== */
int icb_write_mappings(FILE *fb, FILE *fc, icartrom_t *icart, int ofs);

/* ======================================================================== */
/*  ICB_WRITE_PRELOADS                                                      */
/*  Writes the [preload] section of a .CFG, which addresses ranges of       */
/*  address that are preloaded, but not readable.                           */
/* ======================================================================== */
int icb_write_preloads(FILE *fb, FILE *fc, icartrom_t *icart, int ofs);

/* ======================================================================== */
/*  ICB_WRITE_BANKSW                                                        */
/*  Writes the [bankswitch] section.  These are sections marked for         */
/*  Intellicart-style bankswitching.                                        */
/* ======================================================================== */
void icb_write_banksw(FILE *fc, icartrom_t *icart);

/* ======================================================================== */
/*  ICB_WRITE_MEMATTR                                                       */
/*  Writes the [memattr] section.  These are sections marked as RAM.        */
/* ======================================================================== */
void icb_write_memattr(FILE *fc, icartrom_t *icart);

/* ======================================================================== */
/*  ICB_WRITE_BINCFG                                                        */
/*  Write out an entire BIN+CFG.                                            */
/* ======================================================================== */
int icb_write_bincfg(FILE *fb, FILE *fc, icartrom_t *icart, int ofs);

/* ======================================================================== */
/*  ICB_SHOW_SUMMARY                                                        */
/*  Show a bunch of human-readable info about an icartrom.                  */
/* ======================================================================== */
void icb_show_summary(icartrom_t *icart);


/* ======================================================================== */
/*  ROUTINES FOR READING BIN+CFG                                            */
/* ======================================================================== */
typedef struct binseg_t
{
    uint_16     ofs;
    uint_16     addr;
    uint_16     len;
    uint_16     flags;
} binseg_t;


/* ======================================================================== */
/*  PARSE_CFG -- This parses a configuration file and returns a list of     */
/*               sections to load from a file.                              */
/*                                                                          */
/*  Sections of interest in a CFG:                                          */
/*                                                                          */
/*      [mapping]     PRELOAD | READ                                        */
/*      [preload]     PRELOAD                                               */
/*      [bankswitch]  READ | BANKSW                                         */
/*      [memattr]     READ                                                  */
/*      [memattr]     READ | WRITE          (if RAM 16 qualifier)           */
/*      [memattr]     READ | WRITE | NARROW (if RAM 8 qualifier)            */
/*      [memattr]     WRITE                 (if WOM 16 qualifier)           */
/*      [memattr]     WRITE | NARROW        (if WOM 8 qualifier)            */
/*                                                                          */
/*                                                                          */
/*  In the [bankswitch] section, the lines are of the following form:       */
/*                                                                          */
/*      $xxxx - $yyyy                                                       */
/*                                                                          */
/*  Here, $xxxx - $yyyy describes a range of addresses in the INTV          */
/*  address map.  There is no preload data associated with the range.       */
/*                                                                          */
/*                                                                          */
/*  In the [memattr] section, the lines are of the form:                    */
/*                                                                          */
/*      $xxxx - $yyyy = QUAL n                                              */
/*                                                                          */
/*  Here, "QUAL n" is allowed to be one of "RAM 8", "RAM 16", "WOM 8" or    */
/*  "WOM 16".  (WOM 8 and WOM 16 are new, and are being introduced with     */
/*  this tool.)  RAM is used for read/write memory, and WOM is used for     */
/*  write-only memory.                                                      */
/*                                                                          */
/*                                                                          */
/*  In [mapping] and [preload] sections, the lines are of the following     */
/*  format:                                                                 */
/*                                                                          */
/*      $xxxx - $yyyy = $zzzz                                               */
/*                                                                          */
/*  Here, $xxxx - $yyyy describes a range in the BIN file, and $zzzz        */
/*  is the address this place is inserted in the Intellicart memory map.    */
/*                                                                          */
/*                                                                          */
/*  This parser skips other sections, and skips lines starting with any     */
/*  character other than '[' or '$'.  It also ignores anything after a      */
/*  ';' on a line.  The parser is case insensitive, and is not white-space  */
/*  sensitive around '-' and '=' or at the start/end of the lines.  It is   */
/*  sensitive to whitespace inside '[' and ']'.                             */
/* ======================================================================== */
binseg_t *icb_parse_cfg(FILE *f, int bin_len);

/* ======================================================================== */
/*  APPLY_CFG -- Applies segment descriptions to a file image, generating   */
/*               an appropriate icartrom_t.                                 */
/*                                                                          */
/*  Note:  apply_cfg expects that file_img is 64K long even if the file     */
/*  isn't.  It also expects the file_img to be in the proper endian, etc.   */
/* ======================================================================== */
void icb_apply_cfg(binseg_t *segs, uint_16 *file_img, uint_32 img_len, 
                   icartrom_t *icart, int loud);


#endif
/* ======================================================================== */
/*  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 2 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, write to the Free Software             */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               */
/* ======================================================================== */
/*                 Copyright (c) 1998-2001, Joseph Zbiciak                  */
/* ======================================================================== */
