File: bdv.c

package info (click to toggle)
ts10 0.8.021004-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,572 kB
  • ctags: 11,742
  • sloc: ansic: 68,289; makefile: 466
file content (388 lines) | stat: -rwxr-xr-x 10,789 bytes parent folder | download
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// bdv.c - BDV11 Boot/Diagnostics ROM Device
//
// Copyright (c) 2002, Timothy M. Stark
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// TIMOTHY M STARK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Timothy M Stark shall not
// be used in advertising or otherwise to promote the sale, use or other 
// dealings in this Software without prior written authorization from
// Timothy M Stark.

#include "emu/defs.h"
#include "dev/dec/bdv.h"

// *********************** ROM Page 0 ************************

int bdv_ReadROM0(void *dptr, uint32 pAddr, uint16 *data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;
	register uint16  offAddr = bdv->Page0 | (pAddr & 0377);

	*data = (offAddr < bdv->romSize) ?
		((uint16 *)bdv->romImage)[offAddr >> 1] : 0;

	return UQ_OK;
}

int bdv_WriteROM0(void *dptr, uint32 pAddr, uint16 data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;

	// Do nothing

	return UQ_OK;
}

// *********************** ROM Page 1 ************************

int bdv_ReadROM1(void *dptr, uint32 pAddr, uint16 *data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;
	register uint16  offAddr = bdv->Page1 | (pAddr & 0377);

	*data = (offAddr < bdv->romSize) ?
		((uint16 *)bdv->romImage)[offAddr >> 1] : 0;

	return UQ_OK;
}

int bdv_WriteROM1(void *dptr, uint32 pAddr, uint16 data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;

	// Do nothing

	return UQ_OK;
}

// ********************** BDV11 Settings *********************

int bdv_ReadIO(void *dptr, uint32 pAddr, uint16 *data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;
	uint32  reg  = (pAddr >> 1) & 03;

	switch (reg) {
		case nPCR: // Page Control Register
			*data = (bdv->Flags & FLG_KDF11) ? 0101 : PCR;
			break;

		case nSPR: // Scratch Pad Register
			*data = SPR;
			break;

		case nOSR: // Option Select Register
			*data = OSR;
			break;

		default:   // Otherwise - Undefined Registers
			return UQ_NXM;
	}
		
	return UQ_OK;
}

int bdv_WriteIO(void *dptr, uint32 pAddr, uint16 data, uint32 acc)
{
	register BDV_ROM *bdv = (BDV_ROM *)dptr;
	uint32  reg  = (pAddr >> 1) & 03;

	switch (reg) {
		case nPCR: // Page Control Register
			if (acc == ACC_BYTE) // If access is byte, merge with PCR.
				data = (pAddr & 1) ? ((data << 8)   | (PCR & 0377)) :
						               ((data & 0377) | (PCR & ~0377));
			PCR = data & bdv->pcrMask;

			// Update page addresses.
			bdv->Page0 = (data & PCR_MASK) << 8;
			bdv->Page1 = (data & ~PCR_MASK);
			break;

		case nSPR: // Scratch Pad Register
			if (acc == ACC_BYTE) // If access is byte, merge with SPR.
				data = (pAddr & 1) ? ((data << 8)   | (SPR & 0377)) :
						               ((data & 0377) | (SPR & ~0377));
			SPR = data;
			break;

		case nDSP: // Display Register
			if (acc == ACC_BYTE) // If access is byte, merge with SPR.
				data = (pAddr & 1) ? ((data << 8)   | (DSP & 0377)) :
						               ((data & 0377) | (DSP & ~0377));
			DSP = data & 017;
			break;

		default:   // Otherwise - Undefined Registers
			return UQ_NXM;
	}

	return UQ_OK;
}

// Create/Initialize BDV11/KDF11 device.
// Usage: create <device> <BDV11|KDF11> ...
void *bdv_Create(MAP_DEVICE *newMap, int argc, char **argv)
{
	BDV_ROM *bdv = NULL;
	MAP_IO  *io;

	if (bdv = (BDV_ROM *)calloc(1, sizeof(BDV_ROM))) {
		// First, set up its description,
		bdv->Unit.devName    = newMap->devName;
		bdv->Unit.keyName    = newMap->keyName;
		bdv->Unit.emuName    = newMap->emuName;
		bdv->Unit.emuVersion = newMap->emuVersion;

		// Recongize which model - BDV11 or KDF11.
		if (!strcmp(bdv->Unit.keyName, BDV_KEY)) {
			bdv->Flags    = FLG_BDV11;
			bdv->maxSize  = BDV_ROMSZ;
			bdv->pcrMask  = BDV_PCRMASK;
			bdv->pcrWidth = BDV_PCRSZ;
			bdv->osrWidth = BDV_OSRSZ;
		} else if (!strcmp(bdv->Unit.keyName, KDF_KEY)) {
			bdv->Flags    = FLG_KDF11;
			bdv->maxSize  = KDF_ROMSZ;
			bdv->pcrMask  = KDF_PCRMASK;
			bdv->pcrWidth = KDF_PCRSZ;
			bdv->osrWidth = KDF_OSRSZ;
		} else {
			printf("%s: *** Bug Check: Unknown device name - %s\n",
				bdv->Unit.devName, bdv->Unit.keyName);
			free(bdv);
			return NULL;
		}

		bdv->Device     = newMap->devParent->Device;
		bdv->Callback   = newMap->devParent->Callback;
		bdv->System     = newMap->devParent->sysDevice;

		// Set up I/O map settings for BDV11 device.
		io               = &bdv->ioMap;
		io->devName      = bdv->Unit.devName;
		io->keyName      = bdv->Unit.keyName;
		io->emuName      = bdv->Unit.emuName;
		io->emuVersion   = bdv->Unit.emuVersion;
		io->Device       = bdv;
		io->csrAddr      = BDV_CSRADR;
		io->nRegs        = BDV_NREGS;
		io->ReadIO       = bdv_ReadIO;
		io->WriteIO      = bdv_WriteIO;

		// Assign that registers to QBA's I/O space.
		bdv->Callback->SetMap(bdv->Device, io);

		// Set up I/O map settings for ROM Page 0
		io               = &bdv->ioPage0;
		io->devName      = bdv->Unit.devName;
		io->keyName      = bdv->Unit.keyName;
		io->emuName      = bdv->Unit.emuName;
		io->emuVersion   = bdv->Unit.emuVersion;
		io->Device       = bdv;
		io->csrAddr      = ROM_PAGE0;
		io->nRegs        = ROM_WSIZE;
		io->ReadIO       = bdv_ReadROM0;
		io->WriteIO      = bdv_WriteROM0;

		// Assign that registers to QBA's I/O space.
		bdv->Callback->SetMap(bdv->Device, io);

		// Set up I/O map settings for ROM Page 1
		io               = &bdv->ioPage1;
		io->devName      = bdv->Unit.devName;
		io->keyName      = bdv->Unit.keyName;
		io->emuName      = bdv->Unit.emuName;
		io->emuVersion   = bdv->Unit.emuVersion;
		io->Device       = bdv;
		io->csrAddr      = ROM_PAGE1;
		io->nRegs        = ROM_WSIZE;
		io->ReadIO       = bdv_ReadROM1;
		io->WriteIO      = bdv_WriteROM1;

		// Assign that registers to QBA's I/O space.
		bdv->Callback->SetMap(bdv->Device, io);

		// Finally, link it to its mapping device.
		newMap->Device   = bdv;
		newMap->Callback = bdv->Callback;
	}

	return bdv;
}

int bdv_Attach(MAP_DEVICE *map, int argc, char **argv)
{
	BDV_ROM *bdv = (BDV_ROM *)map->Device;
	int     romFile;
	int     szImage;
	int     rc;

	if (bdv->romImage) {
		printf("%s: Already attached.  Please use DETACH first.\n",
			bdv->Unit.devName);
		return EMU_OPENERR;
	}

	if ((romFile = open(argv[2], O_RDONLY)) < 0) {
		printf("%s: File '%s': %s\n",
			bdv->Unit.devName, argv[2], strerror(errno));
		return EMU_OPENERR;
	}

	if ((szImage = lseek(romFile, 0, SEEK_END)) < 0) {
		printf("%s: File 's': %s\n",
			bdv->Unit.devName, argv[2], strerror(errno));
		close(romFile);
		return EMU_OPENERR;
	}
	
	if (szImage > bdv->maxSize) {
		printf("%s: File '%s': Too large ROM image (%d bytes > %d max bytes)\n",
			bdv->Unit.devName, argv[2], szImage, bdv->maxSize);
		close(romFile);
		return EMU_OPENERR;
	}

	bdv->romFile  = (char *)malloc(strlen(argv[2])+1);
	strcpy(bdv->romFile, argv[2]);

	bdv->romImage = (uint8 *)malloc(szImage);
	lseek(romFile, 0, SEEK_SET);
	if ((rc = read(romFile, bdv->romImage, szImage)) < szImage) {
		printf("%s: File '%s': %s\n", bdv->Unit.devName, argv[2],
			(rc < 0) ? strerror(errno) : "Too Short Image - Prematured EOF");
		if (rc >= 0) printf("%s:   %d bytes read (%d bytes expected).\n",
				bdv->Unit.devName, rc, szImage);

		// Release ROM and filename space.
		close(romFile);
		free(bdv->romFile);
		free(bdv->romImage);
		bdv->romFile  = NULL;
		bdv->romImage = NULL;

		return EMU_OPENERR;
	}
	bdv->romSize  = szImage;
	close(romFile);
	OSR = 07773;

	printf("%s: ROM File '%s' had been loaded.\n",
		bdv->Unit.devName, argv[2]);

	return EMU_OK;
}

int bdv_Detach(MAP_DEVICE *map, int argc, char **argv)
{
	BDV_ROM *bdv = (BDV_ROM *)map->Device;

	if (bdv->romImage) {
		// Remove ROM image from BDV11/KDF11 Boot ROM device.
		free(bdv->romImage);
		bdv->romImage = NULL;
		bdv->romSize  = 0;

		// Tell operator that ROM image had been removed.
		printf("%s: ROM File '%s' had been removed.\n",
			bdv->Unit.devName, bdv->romFile);

		// Finally, delete ROM filename and return.
		free(bdv->romFile);
		bdv->romFile = NULL;

		return EMU_OK;
	}

	printf("%s: Already detached.\n", bdv->Unit.devName);
	return EMU_OK;
}

int bdv_Info(MAP_DEVICE *map, int argc, char **argv)
{
	BDV_ROM *bdv = (BDV_ROM *)map->Device;

	printf("\nDevice:       %s  Type: %s\n",
		bdv->Unit.devName, bdv->Unit.keyName);
	printf("ROM File:     %s\n",
		bdv->romImage ? bdv->romFile : "(Not Loaded)");
	printf("ROM Size:     %d of %d bytes\n",
		bdv->romSize, bdv->maxSize);

	return EMU_OK;
}

// BDV11 ROM Device
DEVICE bdv_Device =
{
	BDV_KEY,      // Device Type (Key) Name
	BDV_NAME,     // Emulator Name
	BDV_VERSION,  // Emulator Version

	NULL,         // Listing of devices
	DF_SYSMAP,    // Device Flags
	DT_NETWORK,   // Device Type

	NULL,         // Commands
	NULL,         // Set Commands
	NULL,         // Show Commands

	bdv_Create,   // Create Routine
	NULL,         // Configure Routine
	NULL,         // Delete Routine
	NULL,         // Reset Routine
	bdv_Attach,   // Attach Routine
	bdv_Detach,   // Detach Routine
	bdv_Info,     // Info Routine
	NULL,         // Boot Routine
	NULL,         // Execute Routine
#ifdef DEBUG
	NULL,         // Debug Routine
#endif /* DEBUG */
};

// KDF11 ROM Device for F11 processor
DEVICE kdf_Device =
{
	KDF_KEY,      // Device Type (Key) Name
	BDV_NAME,     // Emulator Name
	BDV_VERSION,  // Emulator Version

	NULL,         // Listing of devices
	DF_SYSMAP,    // Device Flags
	DT_NETWORK,   // Device Type

	NULL,         // Commands
	NULL,         // Set Commands
	NULL,         // Show Commands

	bdv_Create,   // Create Routine
	NULL,         // Configure Routine
	NULL,         // Delete Routine
	NULL,         // Reset Routine
	bdv_Attach,   // Attach Routine
	bdv_Detach,   // Detach Routine
	bdv_Info,     // Info Routine
	NULL,         // Boot Routine
	NULL,         // Execute Routine
#ifdef DEBUG
	NULL,         // Debug Routine
#endif /* DEBUG */
};