.PAS Source to NES Image
Here is the Pascal source code to the first version of NES Image by DiskDude.

{

  NES Image: Split and join a NES image into its ROM and VIDEO parts.

  Note:      This uses the NEW(est) header format as defined below...



                          .                 .

                            ______ ______ .

                         .:_\_ .  \\_ .__\_::.

                      . .::./ ./  // ./__ .:::. .

                         :_<_____/<______>_:.

                         .                 .



    It is a crime to redistribute these routines in a commercial

    venture of any kind without permission or a licensing agreement.

    Contact Damaged Cybernetics via email for more information on

    licensing.



    This is freely distributable for private use, however we require

    that you acknowledge the following:



    Copyright (c) 1996 DiskDude. All rights reserved.

    Copyright (c) 1996 Damaged Cybernetics. All rights reserved.



    If you have any questions or comments, please contact any of the

    following people via the Internet:



      Donald Moore    (MindRape)    moore@futureone.com

      John Pappas     (DiskDude)    papa@connexus.apana.org.au

      Jeremy Chadwick (Yoshi)       yoshi@crystalis.com

      Donald Staheli  (Royce)       staheli@goodnet.com



    World Wide Web: http://www.futureone.com/~damaged/

}

program NES_Image;

uses dos;



const bsize      = 16*1024-1;                 {Use a 16kbyte buffer size}

var buf          : array[0..bsize] of char;   {The actual buffer}

    rom_file,                                 {file handle for the .ROM file}

    vid_file,                                 {file handle for the .VID file}

    src_file     : file;                      {file handle for the .NES file}

    command,                                  {Command: split or join}

    filename     : string;                    {filename without extention}

    rom, vid,

    tmp, result  : integer;

    total        : longint;



procedure close_files;

begin

     {We're done with the files, so close them}

     close(vid_file);

     close(rom_file);

     close(src_file);

end;



procedure NES2RAW;

begin

     {open our file}

     filename:=paramstr(2);

     writeln('NES image: ',filename+'.nes');

     assign(src_file,filename+'.nes');

     reset(src_file,1);

     {Check for file error}

     if ioresult<>0 then

     begin

          writeln('Cannot open file.',#7);

          halt;

     end;

     {Open our CHR and PRG images}

     assign(rom_file,filename+'.rom');

     assign(vid_file,filename+'.vid');

     rewrite(rom_file,1);

     rewrite(vid_file,1);



     writeln('PRG image: ',filename+'.rom');

     writeln('CHR image: ',filename+'.vid');

     writeln;

     writeln('Converting NES image to PRG/CHR images:');

     {

     Format of header: NES^ZABCD00000000



     A = no. of 16kbyte ROM   pages

     B = no. of  8kbyte VIDEO pages NOTE: old images used no. of 16kb ROM pages

     C = cart info byte:



         76543210

              abc



              ab : 00 - none

                   01 - MMC1/LSxxx bin counter.

                   10 - MMC2

                   11 - MMC3

              c  :  0 - A10/A11 not shorted & not connected to V/H

                    1 - A10/A11 shorted & connected to V/H



     D = SRAM size in kbytes (hmph! Marat wants to use the # 16kb

                              pages... boo-sucks to that!) :>

     }

     {Get 16byte header...}

     blockread(src_file,buf,16,result);

     {Check to see if we have a NES image}

     if buf[0]+buf[1]+buf[2]+buf[3]='NES'+#$1a then

     begin

           {Get the number of ROM pages}

           rom:=ord(buf[4]);

           {Get the number of CHR pages}

           vid:=ord(buf[5]);

           {read the number of 16kbyte pages of ROM data}

           total:=0;

           for tmp:=1 to rom do

           begin

                blockread(src_file,buf,sizeof(buf),result);

                blockwrite(rom_file,buf,result);

                inc(total,result);

           end;

           writeln('Wrote ',total,' bytes PRG data...');

           {read the number of 8kbyte pages of VID data}

           total:=0;

           for tmp:=1 to vid do

           begin

                blockread(src_file,buf,8192,result);

                blockwrite(vid_file,buf,result);

                inc(total,result);

           end;

           writeln('Wrote ',total,' bytes CHR data...');

           writeln('Complete.');

     end else writeln('Not a NES image.',#7);

end;



procedure RAW2NES;

begin

     filename:=paramstr(2);

     writeln('PRG image: ',filename+'.rom');

     writeln('CHR image: ',filename+'.vid');

     {open our CHR and PRG images}

     assign(rom_file,filename+'.rom');

     assign(vid_file,filename+'.vid');

     reset(rom_file,1);

     reset(vid_file,1);

     {Check for file error}

     if ioresult<>0 then

     begin

          writeln('Cannot open file(s).',#7);

          halt;

     end;

     {Open our NES image}

     assign(src_file,filename+'.nes');

     rewrite(src_file,1);



     writeln('NES image: ',filename+'.nes');

     writeln;

     writeln('Converting PRG/CHR images to NES image:');





     {Get the number of 16kbyte pages of PRG image}

     rom:=round(filesize(rom_file) / (16*1024));

     {Get the number of 8kbyte pages of CHR image}

     vid:=round(filesize(vid_file) / (8*1024));



     {Sometimes the size of the CHR/PRG file is less than 16/8kbytes -

      make sure we say theres at least 1 page or else we dont read

      any data at all :(                                              }

     if (filesize(rom_file)<>0) and (rom=0) then rom:=1;

     if (filesize(vid_file)<>0) and (vid=0) then vid:=1;



     {PRG must not be larger than 4mbits}

     if (rom*16*1024)>(131072*4) then

     begin

          writeln(filename+'.rom');

          writeln;

          writeln('This file is not a NES PRG image.');

          writeln('NES PRG ROM images are no larger than 4mbit.');

          close_files;

          halt;

     end;



     {CHR must not be larger than 256kbits}

     if (vid*7*1024)>(32*1024) then

     begin

          writeln(filename+'.rom');

          writeln;

          writeln('This file is not a NES CHR image.');

          writeln('NES CHR RAM/ROM images are no larger than 256kbit.');

          close_files;

          halt;

     end;



     {Create our NES header}

     for tmp:=0 to 15 do buf[tmp]:=#0; {clear buffer first}

     buf[0]:='N';

     buf[1]:='E';

     buf[2]:='S';

     buf[3]:=#$1A;

     buf[4]:=chr(rom);

     buf[5]:=chr(vid);

     buf[6]:=#0;

     buf[7]:=#0;       {how are we to know SRAM size and 

                        V/H with just raw images?}



     {write the NES header to disk}

     blockwrite(src_file,buf,16,result);



     {Copy the PRG image to NES image}

     total:=0;

     for tmp:=1 to rom do

     begin

          blockread(rom_file,buf,sizeof(buf),result);

          blockwrite(src_file,buf,result);

          inc(total,result);

     end;

     writeln('Wrote ',total,' bytes PRG data...');

     {Copy the CHR image to NES image}

     total:=0;

     for tmp:=1 to vid do

     begin

          blockread(vid_file,buf,8192,result);

          blockwrite(src_file,buf,result);

          inc(total,result);

     end;

     writeln('Wrote ',total,' bytes CHR data...');

     writeln('Complete!');

end;





begin

     writeln('NES Image: Join/Split NES images.');

     writeln('Copyright (c) 1996 DiskDude. All rights reserved.');

     writeln('Copyright (c) 1996 Damaged Cybernetics. All rights reserved.');

     writeln;

     command:=paramstr(1);

     command:=upcase(command[1]);

     if (paramcount<>2)          or

        (length(paramstr(1))<>1) or

        ((command<>'S')          and

        (command<>'J'))          then

     begin

          writeln('Usage: NesImage [s|j] [filename]');

          writeln;

          writeln(' s          - Split an image');

          writeln(' j          - Join an image');

          writeln(' [filename] - Must NOT include the extention!');

          halt;

     end;



     {Split or Join file...}

     case command[1] of

     'S' : NES2RAW;

     'J' : RAW2NES;

     end;{case}



     close_files;

end.



Damaged Cybernetics is not connected or affiliated with any mentioned company in any way. The opinions of Damaged Cybernetics do not reflect the views of the various companies mentioned here. Companies and all products pertaining to that company are trademarks of that company. Please contact that company for trademark and copyright information.

© 1996 Damaged Cybernetics All Rights Reserved