1 /*
2  *  Copyright (C) 2005-2009  Anders Gavare.  All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions are met:
6  *
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. The name of the author may not be used to endorse or promote products
13  *     derived from this software without specific prior written permission.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  *
28  *  COMMENT: LPT (parallel printer) controller
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "console.h"
36 #include "cpu.h"
37 #include "device.h"
38 #include "interrupt.h"
39 #include "machine.h"
40 #include "memory.h"
41 #include "misc.h"
42 
43 #include "thirdparty/lptreg.h"
44 
45 
46 /*  #define debug fatal  */
47 
48 #define	TICK_SHIFT		18
49 #define	DEV_LPT_LENGTH		3
50 
51 struct lpt_data {
52 	const char		*name;
53 	struct interrupt	irq;
54 	int			console_handle;
55 
56 	uint8_t			data;
57 	uint8_t			control;
58 };
59 
60 
DEVICE_TICK(lpt)61 DEVICE_TICK(lpt)
62 {
63 	/*  struct lpt_data *d = extra;  */
64 
65 	/*  TODO  */
66 }
67 
68 
DEVICE_ACCESS(lpt)69 DEVICE_ACCESS(lpt)
70 {
71 	uint64_t idata = 0, odata=0;
72 	struct lpt_data *d = (struct lpt_data *) extra;
73 
74 	if (writeflag == MEM_WRITE)
75 		idata = memory_readmax64(cpu, data, len);
76 
77 	switch (relative_addr) {
78 
79 	case LPT_DATA:
80 		if (writeflag == MEM_READ)
81 			odata = d->data;
82 		else
83 			d->data = idata;
84 		break;
85 
86 	case LPT_STATUS:
87 		odata = LPS_NBSY | LPS_NACK | LPS_SELECT | LPS_NERR;
88 		break;
89 
90 	case LPT_CONTROL:
91 		if (writeflag == MEM_WRITE) {
92 			if (idata != d->control) {
93 				if (idata & LPC_STROBE) {
94 					/*  data strobe  */
95 					console_putchar(d->console_handle,
96 					    d->data);
97 				}
98 			}
99 			d->control = idata;
100 		}
101 		break;
102 	}
103 
104 	if (writeflag == MEM_READ)
105 		memory_writemax64(cpu, data, len, odata);
106 
107 	return 1;
108 }
109 
110 
DEVINIT(lpt)111 DEVINIT(lpt)
112 {
113 	struct lpt_data *d;
114 	size_t nlen;
115 	char *name;
116 
117 	CHECK_ALLOCATION(d = (struct lpt_data *) malloc(sizeof(struct lpt_data)));
118 	memset(d, 0, sizeof(struct lpt_data));
119 
120 	INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
121 
122 	d->name		= devinit->name2 != NULL? devinit->name2 : "";
123 	d->console_handle = console_start_slave(devinit->machine, devinit->name,
124 	    CONSOLE_OUTPUT_ONLY);
125 
126 	nlen = strlen(devinit->name) + 10;
127 	if (devinit->name2 != NULL)
128 		nlen += strlen(devinit->name2);
129 	CHECK_ALLOCATION(name = (char *) malloc(nlen));
130 	if (devinit->name2 != NULL && devinit->name2[0])
131 		snprintf(name, nlen, "%s [%s]", devinit->name, devinit->name2);
132 	else
133 		snprintf(name, nlen, "%s", devinit->name);
134 
135 	memory_device_register(devinit->machine->memory, name, devinit->addr,
136 	    DEV_LPT_LENGTH, dev_lpt_access, d, DM_DEFAULT, NULL);
137 	machine_add_tickfunction(devinit->machine, dev_lpt_tick, d,
138 	    TICK_SHIFT);
139 
140 	/*
141 	 *  NOTE:  Ugly cast into a pointer, because this is a convenient way
142 	 *         to return the console handle to code in src/machine.c.
143 	 */
144 	devinit->return_ptr = (void *)(size_t)d->console_handle;
145 
146 	return 1;
147 }
148 
149