Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / ss / lib / cpu / src / SS_FastMemory.cc
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: SS_FastMemory.cc
4// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
6//
7// The above named program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public
9// License version 2 as published by the Free Software Foundation.
10//
11// The above named program is distributed in the hope that it will be
12// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public
17// License along with this work; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19//
20// ========== Copyright Header End ============================================
21
22#ifdef COMPILE_FOR_SAM
23
24
25#else // Vonk's own memory
26
27#include <sys/mman.h>
28#include <errno.h>
29#include <ctype.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <assert.h>
34#include <unistd.h>
35#include "SS_FastMemory.h"
36
37SS_FastMemory SS_FastMemory::memory;
38
39SS_FastMemory::SS_FastMemory()/*{{{*/
40{
41 page_size = sysconf(_SC_PAGESIZE);
42 page_mask = page_size-1;
43
44 // Note mmap with MAP_ANON zero's memory for us. Doing mmap here
45 // in stead of calloc safes us a bundle of page faults. Besides
46 // the initial memory footprint now very small.
47
48 l1 = (uint8_t**)mmap((char*)0,(1ull << L1BITS) * sizeof(uint8_t*),PROT_READ|PROT_WRITE,
49 MAP_NORESERVE|MAP_PRIVATE|MAP_ANON|MAP_ALIGN,-1,0);
50}
51/*}}}*/
52SS_FastMemory::~SS_FastMemory()/*{{{*/
53{
54 // We should in to be nice and do unmap of all the mmap's
55 // here, but what is the point of that .... we're gonna
56 // exit anyways. Worse, if we do this then we incur
57 // quite a few page misses at the end of the program,
58 // which means exit seems slow.
59}
60/*}}}*/
61void SS_FastMemory::allocate( uint64_t _ram_size, uint64_t _rom_size, uint_t pa_bits )/*{{{*/
62{
63 ram_size = _ram_size;
64 rom_size = _rom_size;
65}
66/*}}}*/
67
68#include <zlib.h>
69
70enum { BUFFER_SIZE = 512 };
71static char buffer[BUFFER_SIZE];
72
73void SS_FastMemory::load( const char* filename )/*{{{*/
74{
75 const char* separ = " \n";
76 gzFile image;
77 uint64_t addr = 0;
78 bool gunzip = false;
79 uint_t len = strlen(filename);
80
81 image = gzopen(filename,"r");
82
83 if (!image)
84 throw "No such ascii memory image file found.";
85
86 while (gzgets(image,buffer,BUFFER_SIZE))
87 {
88 if (buffer[0] == '@')
89 {
90 addr = strtoull(buffer+1,0,16);
91 }
92 else if (isxdigit(buffer[0]))
93 {
94 char* token = strtok(buffer,separ);
95 uint_t length = strlen(token);
96
97 if (length == 16)
98 {
99 do
100 {
101 uint64_t data = strtoull(token,0,16);
102 poke64(addr,data);
103 addr += 8;
104 }
105 while ((token = strtok(0,separ)));
106 }
107 else if (length == 8)
108 {
109 do
110 {
111 uint32_t data = strtoul(token,0,16);
112 poke32(addr,data);
113 addr += 4;
114 }
115 while ((token = strtok(0,separ)));
116 }
117 else
118 throw "A memory entry must be either 4 or 8 bytes.";
119 }
120 }
121
122 gzclose(image);
123}
124/*}}}*/
125void SS_FastMemory::load( const char* filename, uint64_t addr )/*{{{*/
126{
127 FILE* image = fopen(filename,"r");
128
129 if (!image)
130 throw "No such binary memory image file found.";
131
132 if ((addr & page_mask) != 0)
133 {
134 fread(get_ptr(addr),1,page_size - (addr & page_mask),image);
135 addr = (addr + page_size) &~ page_mask;
136 }
137 while (!feof(image))
138 {
139 fread(get_ptr(addr),1,page_size,image);
140 addr += page_size;
141 }
142
143 fclose(image);
144}
145/*}}}*/
146void SS_FastMemory::save( const char* filename, uint64_t addr, uint64_t size )/*{{{*/
147{
148 FILE* image = fopen(filename,"w");
149
150 if (!image)
151 throw "No such binary memory image file found.";
152
153 if ((addr & page_mask) != 0)
154 {
155 uint64_t n = page_size - (addr & page_mask);
156 if (size < n)
157 n = size;
158 fwrite(get_ptr(addr),1,n,image);
159 addr += n;
160 size -= n;
161 }
162 while (size >= page_size)
163 {
164 fwrite(get_ptr(addr),1,page_size,image);
165 addr += page_size;
166 size -= page_size;
167 }
168 if (size)
169 fwrite(get_ptr(addr),1,size,image);
170
171 fclose(image);
172}
173/*}}}*/
174
175#endif /* COMPILE_FOR_SAM */