std_shmem.c
6.02 KB
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
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* File: std_shmem.c
* Creator: hsa@sgi.com
* Create Date: Fri Mar 10 10:43:35 PST 1995
*
* This file implements a shared-memory interface to the RSP C
* simulator. With this, we can connect the simulator directly
* to an R4300 simulator.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <signal.h>
#if 0
#include <errno.h>
extern int errno;
#endif
#include <sys/mman.h>
#include <fcntl.h>
#include <PR/rsp_ipc.h>
#include <PR/os.h> /* for signal */
#include <PR/em.h> /* for signal */
#include <errno.h>
#include "rsp.h"
#include "rspctl.h"
#include "memory.h"
/*
* Define this for more verbose messages...
#define SP_VERBOSE 1
*/
#ifndef KSEG0_BASE
# define KSEG0_BASE (0x20000000)
#endif
static int semid;
static rsp_shm_buf_t *bufptr = (rsp_shm_buf_t *) -1;
/*
* initialize the shared memory interface.
*/
void
rsp_ShmemInit(int mem_key, char *dramFile)
{
int sem_key, dramfd;
sem_key = mem_key + 10;
#ifdef SP_VERBOSE
fprintf(stderr,"rsp : mem_key = %d sem_key = %d\n",
mem_key, sem_key);
#endif
/*
* To simulate DRAM, create a shared mapped file that can be used
* by the emulator and the rsp simulator.
*/
if ((dramfd = open(dramFile, O_RDWR, 0600)) == -1) {
fprintf(stderr, "rsp : cannot create temp file %s (%s)\n",
dramFile, strerror(errno));
exit(1);
}
if (mmap((void *)KSEG0_BASE, sizeof(rsp_shm_buf_t), PROT_READ|PROT_WRITE|PROT_EXEC,
#ifdef __linux__
MAP_FIXED|MAP_SHARED,
#else
MAP_FIXED|MAP_SHARED|MAP_AUTOGROW,
#endif
dramfd, 0) == (void *)-1) {
fprintf(stderr,
"rsp : mmap(0x%x, 0x%x) of temp file failed (%s)\n",
KSEG0_BASE, sizeof(rsp_shm_buf_t),
strerror(errno));
exit(1);
}
bufptr = (rsp_shm_buf_t *) KSEG0_BASE;
bufptr->cmd = CMD_NULL;
/*
* open a semaphore.
*/
if ((semid = sem_open(sem_key)) < 0) {
fprintf(stderr,"%s : can't open semaphore.\n", "rsp");
exit(-1);
}
/*
* re-direct DRAM:
*/
rsp_mainMemory = (u8 *) &(bufptr->dram[0]);
}
/*
* take over the RSP. Does not return (until the 'quit' command)
*/
void
rsp_ShmemRun()
{
int docont = 1;
char cmd[32], oldline[256], regsym[32];
int lastcmd = -1;
/*
* this loop becomes the clock loop, calling ProcessorStep()
* when the RSP is running.
*
*/
#ifdef SP_VERBOSE
fprintf(stderr,"rsp command loop starting\n");
#endif
while (docont) {
#ifdef SP_VERBOSE
if (bufptr->cmd != lastcmd) {
fprintf(stderr,"rsp : cmd = %d\n",bufptr->cmd);
lastcmd = bufptr->cmd;
}
#endif
/* test (read) the command with out semaphores... */
if (bufptr->cmd != CMD_NULL) {
/* new command, lock semaphore and process: */
sem_wait(semid);
if (bufptr->cmd == CMD_QUIT) {
fprintf(stdout,"rsp server: received quit command...");
docont = 0;
} else if (bufptr->cmd == CMD_CMD) {
/* check for null command (repeat last command) */
if (bufptr->string[0] != '\n') {
strcpy(oldline, bufptr->string);
}
#ifdef SP_VERBOSE
fprintf(stderr,"rsp : server command [%s]\n",oldline);
#endif
/* pick off first thing typed: */
sscanf(oldline,"%s",cmd);
if ((strncmp(cmd, "step", strlen(cmd)) == 0) ||
(strncmp(cmd, "run", strlen(cmd)) == 0)) {
/* don't let 'run' or 'step' commands through */
} else {
process_command(cmd, oldline, NULL);
}
} else if (bufptr->cmd == CMD_DMA_READ) {
/* read memory */
/* copy 'length' bytes from 'addr' to 'buff' */
rsp_MemToIPC(bufptr->addr, (u32) &(bufptr->buff[0]),
bufptr->length);
} else if (bufptr->cmd == CMD_DMA_WRITE) {
/* write memory */
/* copy 'length' bytes from 'buff' to 'addr' */
rsp_MemFromIPC((u32) &(bufptr->buff[0]), bufptr->addr,
bufptr->length);
} else if (bufptr->cmd == CMD_READ_WORD) {
bufptr->buff[0] = rsp_MemReadMap(bufptr->addr);
} else if (bufptr->cmd == CMD_WRITE_WORD) {
rsp_MemWriteMap(bufptr->addr, bufptr->buff[0]);
} else {
fprintf(stderr,"rsp : BOGUS COMMAND!\n");
}
bufptr->cmd = CMD_NULL;
sem_signal(semid);
}
/*
* check for somebody that has initiated 'single-step'.
*/
if (!Flagged(rsp_controlReg, rsp_CtlHaltMask) && docont &&
Flagged(rsp_controlReg, SP_STATUS_SSTEP)) {
#ifdef SP_VERBOSE
fprintf(stderr,"rsp : in single step mode\n");
#endif
rsp_ProcessorSStep();
rsp_controlReg = Flag(rsp_controlReg, SP_STATUS_HALT);
rsp_controlReg = Flag(rsp_controlReg, SP_STATUS_BROKE);
}
if (!Flagged(rsp_controlReg, rsp_CtlHaltMask) && docont) {
rsp_ProcessorStep();
#ifdef SP_VERBOSE
fprintf(stderr,"rsp : step?\n");
#endif
/*
* Detect a break.
*/
#ifdef SP_VERBOSE
fprintf(stderr,"about to check break...\n");
#endif
if (!Flagged(rsp_controlReg, SP_STATUS_SSTEP) &&
Flagged(rsp_controlReg, rsp_CtlHaltMask)) {
#ifdef SP_VERBOSE
fprintf(stderr,"rsp : BREAK!!!!!!!!!!!!!!!!\n");
#endif
fflush(stderr);
kill(getppid(), SIGSP);
}
}
}
/*
* close the semaphore.
*/
sem_close(semid);
fprintf(stdout,"[rsp server] exiting.\n");
fflush(stdout);
/*
* So the emulator exits first, avoiding SIGCHLD problems...
*/
sleep(10);
}