1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * ␉␉␉INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | *␉This software is supplied under the terms of a license agreement or ␊ |
36 | *␉nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | *␉nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | *␉Copyright 1988, 1989 Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | #include "libsaio.h"␊ |
48 | ␊ |
49 | /*␊ |
50 | * keyboard controller (8042) I/O port addresses␊ |
51 | */␊ |
52 | #define PORT_A 0x60 /* port A */␊ |
53 | #define PORT_B 0x64 /* port B */␊ |
54 | ␊ |
55 | /*␊ |
56 | * keyboard controller command␊ |
57 | */␊ |
58 | #define CMD_WOUT 0xd1 /* write controller's output port */␊ |
59 | ␊ |
60 | /*␊ |
61 | * keyboard controller status flags␊ |
62 | */␊ |
63 | #define KB_INFULL 0x2 /* input buffer full */␊ |
64 | #define KB_OUTFULL 0x1 /* output buffer full */␊ |
65 | ␊ |
66 | #define KB_A20 0x9f /* enable A20,␊ |
67 | enable output buffer full interrupt␊ |
68 | enable data line␊ |
69 | disable clock line */␊ |
70 | ␊ |
71 | //==========================================================================␊ |
72 | // Enable A20 gate to be able to access memory above 1MB␊ |
73 | static inline void flushKeyboardInputBuffer();␊ |
74 | static inline void flushKeyboardInputBuffer()␊ |
75 | {␊ |
76 | unsigned char ret;␊ |
77 | /* Apparently all flags on means that they're invalid and that the code␊ |
78 | should stop trying to check them because they'll never change */␊ |
79 | do␊ |
80 | {␊ |
81 | ret = inb(PORT_B);␊ |
82 | } while( (ret != 0xff) && (ret & KB_INFULL));␊ |
83 | }␊ |
84 | ␊ |
85 | void enableA20()␊ |
86 | {␊ |
87 | /* make sure that the input buffer is empty */␊ |
88 | flushKeyboardInputBuffer();␊ |
89 | ␊ |
90 | /* make sure that the output buffer is empty */␊ |
91 | if (inb(PORT_B) & KB_OUTFULL)␊ |
92 | (void)inb(PORT_A);␊ |
93 | ␊ |
94 | /* make sure that the input buffer is empty */␊ |
95 | flushKeyboardInputBuffer();␊ |
96 | ␊ |
97 | /* write output port */␊ |
98 | outb(PORT_B, CMD_WOUT);␊ |
99 | delay(100);␊ |
100 | ␊ |
101 | /* wait until command is accepted */␊ |
102 | flushKeyboardInputBuffer();␊ |
103 | ␊ |
104 | outb(PORT_A, KB_A20);␊ |
105 | delay(100);␊ |
106 | ␊ |
107 | /* wait until done */␊ |
108 | flushKeyboardInputBuffer();␊ |
109 | }␊ |
110 | #if UNUSED␊ |
111 | void turnOffFloppy(void)␊ |
112 | {␊ |
113 | ␉/*␊ |
114 | ␉ * Disable floppy:␊ |
115 | ␉ * Hold controller in reset,␊ |
116 | ␉ * disable DMA and IRQ,␊ |
117 | ␉ * turn off floppy motors.␊ |
118 | ␉ */␊ |
119 | ␉outb(0x3F2, 0x00);␊ |
120 | }␊ |
121 | #endif␊ |
122 | ␊ |
123 | static bool fix_random=true;␊ |
124 | ␊ |
125 | struct ran_obj* random_init (int rmin, int rmax)␊ |
126 | {␊ |
127 | ␉if (rmin > rmax) ␊ |
128 | ␉␉return false;␊ |
129 | ␉␊ |
130 | int n = (rmax+1) - rmin;␊ |
131 | ␉␊ |
132 | ␉int tab[rand_tab_len];␊ |
133 | ␉␊ |
134 | ␉struct ran_obj * self = (struct ran_obj * )malloc(sizeof(struct ran_obj));␉␊ |
135 | ␉␊ |
136 | bzero(self,sizeof(struct ran_obj));␊ |
137 | ␊ |
138 | ␉self->rmin= rmin;␊ |
139 | ␉␊ |
140 | ␉self->n= n;␊ |
141 | ␉␊ |
142 | ␉int i;␊ |
143 | ␉srand(time18());␊ |
144 | ␉int limit1= 0, limit2= 0, gate1 = 0, gate2 = 0;␊ |
145 | ␉␊ |
146 | ␉if (fix_random) {␊ |
147 | ␉␉limit1 = (rand() % 20) ;␉␉␊ |
148 | ␉␉limit2 = (rand() % 20) ;␊ |
149 | ␉}␊ |
150 | ␉␊ |
151 | ␉for (i = 0; i < rand_tab_len; i++){␊ |
152 | ␉␉␊ |
153 | ␉␉tab[i] = (rand() % n) + rmin;␊ |
154 | ␉␉␊ |
155 | ␉␉if (fix_random) {␉␉␉␊ |
156 | ␉␉␉␊ |
157 | ␉␉␉if (i > 1 && gate1 < limit1 && tab[i]==tab[i-2]) {␊ |
158 | ␉␉␉␉i--;␊ |
159 | ␉␉␉␉gate1++;␊ |
160 | ␉␉␉␉␊ |
161 | ␉␉␉␉continue;␊ |
162 | ␉␉␉}␊ |
163 | ␉␉␉␊ |
164 | ␉␉␉if (i > 7 && gate2 < limit2 && tab[i]==tab[i-((rand() % 4)+5)]) {␊ |
165 | ␉␉␉␉i--;␊ |
166 | ␉␉␉␉gate2++;␊ |
167 | ␉␉␉␉␊ |
168 | ␉␉␉␉continue;␊ |
169 | ␉␉␉}␊ |
170 | ␉␉␉␊ |
171 | ␉␉}␊ |
172 | ␉␉self->tab[i]= tab[i];␉␉␊ |
173 | ␉}␊ |
174 | ␉␊ |
175 | ␉␊ |
176 | ␉return self;␊ |
177 | }␊ |
178 | ␊ |
179 | int random (struct ran_obj* self)␊ |
180 | {␊ |
181 | ␉␊ |
182 | ␉//struct ran_opt * ret = (struct ran_opt * )malloc(sizeof(struct ran_opt));␊ |
183 | ␉//ret = rt ;␊ |
184 | ␉␊ |
185 | struct ran_obj * ret = self ;␊ |
186 | ␊ |
187 | ␉static int wheel = 0;␊ |
188 | ␉int gate3 = 0;␊ |
189 | ␉int limit3 = 0;␉␊ |
190 | ␉static int retlst[rand_tab_len];␊ |
191 | ␉␊ |
192 | ␉␊ |
193 | ␉int index;␊ |
194 | ␉int rn;␉␊ |
195 | ␉␊ |
196 | if (!ret) {␊ |
197 | ␉␉return -1;␉// TODO: fix this␉␊ |
198 | ␉}␊ |
199 | ␊ |
200 | ␉if (fix_random) {␊ |
201 | ␉␉gate3 = rand() % 2;␉␉␊ |
202 | ␉}␊ |
203 | ␉␊ |
204 | ␉␊ |
205 | retry:␉␊ |
206 | ␉index = rand() % rand_tab_len;␉␊ |
207 | ␉rn = ret->tab[index];␉␊ |
208 | ␉ret->tab[index] = (rand() % ret->n) + ret->rmin;␊ |
209 | ␉␊ |
210 | ␉␊ |
211 | ␉if (fix_random) {␉␊ |
212 | ␉␉if ((gate3 && limit3 < 5) && (ret->tab[index] == rn)) {␊ |
213 | ␉␉␉limit3++;␊ |
214 | ␉␉␉goto retry;␊ |
215 | ␉␉}␉␊ |
216 | ␉␉retlst[wheel] = rn;␊ |
217 | ␉␉␊ |
218 | ␉␉if (wheel > 0 && limit3 < 5) {␉␊ |
219 | ␉␉␉␊ |
220 | ␉␉␉if (gate3 && (rn == retlst[wheel-1])) {␉␉␉␊ |
221 | ␉␉␉␉limit3++;␊ |
222 | ␉␉␉␉goto retry;␊ |
223 | ␉␉␉}␊ |
224 | ␉␉␉␊ |
225 | ␉␉␉if (gate3 && (wheel > 3 && rn==retlst[wheel-((rand() % 3)+1)]) && (limit3 < 5)) {␉␉␉␊ |
226 | ␉␉␉␉limit3++;␊ |
227 | ␉␉␉␉goto retry;␊ |
228 | ␉␉␉}␊ |
229 | ␉␉}␊ |
230 | ␉␉␊ |
231 | ␉␉wheel++;␊ |
232 | ␉}␊ |
233 | ␉␊ |
234 | ␉self = ret;␉␊ |
235 | ␉return rn;␉␊ |
236 | }␊ |
237 | ␊ |
238 | void random_free (struct ran_obj* self)␊ |
239 | {␊ |
240 | if (self /* && self->sgn == random_obj_sgn */) {␊ |
241 | free(self);␊ |
242 | } ␊ |
243 | ␊ |
244 | }␊ |
245 | ␊ |
246 | void usefixedrandom (bool opt)␊ |
247 | {␊ |
248 | ␉fix_random = opt;␊ |
249 | }␊ |
250 | ␊ |
251 | //==========================================================================␊ |
252 | // Return the platform name for this hardware.␊ |
253 | //␊ |
254 | #ifndef BOOT1␊ |
255 | void␊ |
256 | getPlatformName(char *nameBuf)␊ |
257 | {␊ |
258 | strcpy(nameBuf, "ACPI");␊ |
259 | }␊ |
260 | #endif␊ |
261 | |