File: lp.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 (285 lines) | stat: -rwxr-xr-x 7,412 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
// lp.c - LP11/LS11/LA11 Line Printer Emulation
//
// 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/defs.h"
#include "dev/dec/lp.h"

void lp_Service(void *dptr)
{
	LP_DEVICE *lp = (LP_DEVICE *)dptr;
	MAP_IO    *io = &lp->ioMap;
	uchar     ch;
	int       err;

	// Print a character.
	if (lp->File) {
		ch = lp->lpdb & 0177;
		if ((err = write(lp->File, &ch, 1)) <= 0) {
			lp->lpcsr |= CSR_ERR;
			if (err < 0)
				printf("%s: *** Error: %s\n", lp->devName, strerror(errno));
		}
	}

	// Update CSR flags and ring its host.
	lp->lpcsr |= CSR_DONE;
	if (lp->lpcsr & CSR_IE) {
		io->SendInterrupt(io, 0);
#ifdef DEBUG
		if (dbg_Check(DBG_INTERRUPT))
			dbg_Printf("%s: Ring host\n", lp->devName);
#endif /* DEBUG */
	}
}

int lp_ReadIO(void *dptr, uint32 pAddr, uint16 *data, uint32 size)
{
	LP_DEVICE *lp = (LP_DEVICE *)dptr;
	uint32    reg = (pAddr - (lp->csrAddr & 0x1FFF)) >> 1;

	switch (reg) {
		case nLPCSR:
			*data = lp->lpcsr;
			break;

		case nLPDB:
			*data = lp->lpdb;
			break;

		default:
#ifdef DEBUG
			if (dbg_Check(DBG_IOREGS))
				dbg_Printf("%s: (R) Undefined Register (%06)\n",
					lp->keyName, pAddr & 0x1FFF);
#endif /* DEBUG */
			*data = 0;
			return UQ_NXM;
	}

#ifdef DEBUG
	if (dbg_Check(DBG_IOREGS))
		dbg_Printf("%s: (R) %s (%06o) => %06o\n",
			lp->keyName, regName[reg], pAddr & 0x1FFF, *data);
#endif /* DEBUG */

	return UQ_OK;
}

int lp_WriteIO(void *dptr, uint32 pAddr, uint16 data, uint32 size)
{
	LP_DEVICE *lp = (LP_DEVICE *)dptr;
	MAP_IO    *io = &lp->ioMap;
	uint32    reg = (pAddr - (lp->csrAddr & 0x1FFF)) >> 1;

	switch (reg) {
		case nLPCSR:
			if (pAddr & 1)
				break;

			// Update interrupt flags.
			if ((data & CSR_IE) == 0)
				io->CancelInterrupt(io, 0);
			else if ((lp->lpcsr & (CSR_DONE|CSR_IE)) == CSR_DONE) {
				io->SendInterrupt(io, 0);
#ifdef DEBUG
				if (dbg_Check(DBG_INTERRUPT))
					dbg_Printf("%s: Ring Doorbell\n", lp->devName);
#endif /* DEBUG */
			}

			lp->lpcsr = (data & CSR_RW) | (lp->lpcsr & ~CSR_RW);
			break;

		case nLPDB:
			if ((pAddr & 1) == 0) {
				lp->lpdb   = data & DB_DATA;
				lp->lpcsr &= ~CSR_DONE;
				ts10_SetTimer(&lp->svcTimer);
			}
			break;

#ifdef DEBUG
		default:
			if (dbg_Check(DBG_IOREGS))
				dbg_Printf("%s: (W) Undefined Register (%06)\n",
					lp->keyName, pAddr & 0x1FFF);
			return UQ_NXM;
#endif /* DEBUG */
	}

#ifdef DEBUG
	if (dbg_Check(DBG_IOREGS))
		dbg_Printf("%s: (W) %s (%06o) <= %06o\n",
			lp->keyName, regName[reg], pAddr & 0x1FFF, data);
#endif /* DEBUG */

	return UQ_OK;
}

// ******************************************************************

int lp_Reset(void *);
void *lp_Create(MAP_DEVICE *newMap, int argc, char **argv)
{
	LP_DEVICE *lp = NULL;
	CLK_QUEUE *newTimer;
	MAP_IO    *io;

	if (lp = (LP_DEVICE *)calloc(1, sizeof(LP_DEVICE))) {
		lp->csrAddr    = LP_IOADDR;

		// Set up service timer for transmit
		// characters to the printer.
		newTimer           = &lp->svcTimer;
		newTimer->Next     = NULL;
		newTimer->Flags    = 0;
		newTimer->outTimer = LP_TIMER;
		newTimer->nxtTimer = LP_TIMER;
		newTimer->Device   = (void *)lp;
		newTimer->Execute  = lp_Service;

		// Set up I/O Entry
		io               = &lp->ioMap;
		io->devName      = newMap->devName;
		io->keyName      = newMap->keyName;
		io->emuName      = newMap->emuName;
		io->emuVersion   = newMap->emuVersion;
		io->Device       = lp;
		io->csrAddr      = lp->csrAddr;
		io->nRegs        = LP_NREGS;
		io->intIPL       = LP_IPL;
		io->nVectors     = LP_NVECS;
		io->intVector[0] = LP_VEC;
		io->ReadIO       = lp_ReadIO;
		io->WriteIO      = lp_WriteIO;

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

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

		lp_Reset(lp);

		// Finally, set up its descriptions and
		// link it to its mapping device.
		lp->devName    = newMap->devName;
		lp->keyName    = newMap->keyName;
		lp->emuName    = newMap->emuName;
		lp->emuVersion = newMap->emuVersion;
		newMap->Device = lp;
	}

	return lp;
}

int lp_Reset(void *dptr)
{
	LP_DEVICE *lp = (LP_DEVICE *)dptr;

	ts10_CancelTimer(&lp->svcTimer);

	lp->lpcsr = CSR_DONE | ((lp->File == 0) ? CSR_ERR : 0);
	lp->lpdb  = 0;

	return EMU_OK;
}

int lp_Attach(MAP_DEVICE *map, int argc, char **argv)
{
	LP_DEVICE *lp  = (LP_DEVICE *)map->Device;

	if ((lp->File = open(argv[2], O_WRONLY, 0700)) < 0) {
		printf("%s: file '%s' not attached - %s.\n",
			lp->devName, argv[2], strerror(errno));
		return EMU_OPENERR;
	}

	// Tell operator that and save it.
	printf("%s: file '%s' attached.\n", lp->devName, argv[2]);
	if (lp->fileName = (char *)malloc(strlen(argv[2])+1)) {
		// Put filename on file and update CSR flags.
		strcpy(lp->fileName, argv[2]);
		lp->lpcsr &= ~CSR_ERR;
	} else {
		printf("%s: Can't assign the name of '%s' - Not enough memory.\n",
			map->devName, argv[2]);
	}

	return EMU_OK;
}

int lp_Detach(MAP_DEVICE *map, int argc, char **argv)
{
	LP_DEVICE *lp = (LP_DEVICE *)map->Device;

	if (lp->File) {
		close(lp->File);
		lp->File = 0;

		// Tell operator that.
		printf("%s: file '%s' detached.\n", lp->devName,
			lp->fileName ? lp->fileName : "<Unknown filename>");

		// Free the allocation of filename if available.
		if (lp->fileName)
			free(lp->fileName);
		lp->fileName = NULL;

		// Update CSR flags
		lp->lpcsr |= CSR_ERR;
	} else
		printf("%s: Already detached.\n", lp->devName);

	return EMU_OK;
}

DEVICE lp_Device =
{
	LP_KEY,        // Device Type Name
	LP_NAME,       // Emulator Name
	LP_VERSION,    // Emulator Version
	NULL,          // Listing of Devices
	DF_SYSMAP,     // Device Flags
	DT_DEVICE,     // Device Type

	NULL, NULL, NULL,

	lp_Create,     // Create Routine
	NULL,          // Configure Routine
	NULL,          // Delete Routine
	lp_Reset,      // Reset Routine
	lp_Attach,     // Attach Routine
	lp_Detach,     // Detach Routine
	NULL,          // Info Routine
	NULL,          // Boot Routine    - Not Used
	NULL,          // Execute Routine - Not Used
#ifdef DEBUG
	NULL,          // Debug Routine
#endif /* DEBUG */
};