1 /* Memory-mapped ROM module emulation.
2    Copyright 2003 Brian R. Gaeke.
3 
4 This file is part of VMIPS.
5 
6 VMIPS 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 2 of the License, or (at your
9 option) any later version.
10 
11 VMIPS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with VMIPS; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19 
20 #include "rommodule.h"
21 #include "fileutils.h"
22 #include "mmapglue.h"
23 #include <cerrno>
24 
ROMModule(FILE * fp)25 ROMModule::ROMModule (FILE *fp) : Range (0, 0, 0, MEM_READ) {
26   extent = get_file_size (fp);
27 
28   // Try to map the file into memory. We use PROT_READ to indicate
29   // read-only access.
30   address = mmap (0, extent, PROT_READ, MAP_PRIVATE, fileno (fp), ftell (fp));
31   int errcode = errno;
32   if (address == MAP_FAILED)
33     throw errcode;
34 }
35 
~ROMModule()36 ROMModule::~ROMModule () {
37   munmap (address, extent);
38 }
39