-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREDEYE.C
312 lines (259 loc) · 7.93 KB
/
REDEYE.C
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
/*
* redeye.c
*
* This file is part of Emu42
*
* Copyright (C) 2011 Christoph Gießelink
*
*/
#include "pch.h"
#include "Emu42.h"
// #define DEBUG_REDEYE_CYCLES // switch for redeye cycles debug purpose
// #define DEBUG_REDEYE_DATA // switch for redeye data debug purpose
#define ERR_CHAR 127 // character for transfer error
#define H1 0x78
#define H2 0xE6
#define H3 0xD5
#define H4 0x8B
// HP redeye correction masks
static CONST BYTE byEmask[] = { H1, H2, H3, H4 };
static __inline UINT MAX(UINT a, UINT b)
{
return (a>b)?a:b;
}
static __inline BYTE Parity(BYTE b)
{
b ^= (b >> 4);
b ^= (b >> 2);
b ^= (b >> 1);
return b & 1;
}
static __inline BYTE CreateCorrectionBits(BYTE b)
{
UINT i;
BYTE byVal = 0;
for (i = 0; i < ARRAYSIZEOF(byEmask);++i)
{
byVal <<= 1;
byVal |= Parity((BYTE) (b & byEmask[i]));
}
return byVal;
}
static __inline WORD CorrectData(WORD wData,WORD wMissed)
{
while ((wMissed & 0xFF) != 0) // clear every missed bit in data area
{
BYTE byBitMask;
// detect valid H(i) mask
WORD wMi = 0x800; // first M(i) bit
INT i = 0; // index to first H(i) mask
while (TRUE)
{
if ((wMissed & wMi) == 0) // possible valid mask
{
_ASSERT(i < ARRAYSIZEOF(byEmask));
// select bit to correct
byBitMask = wMissed & byEmask[i];
if (Parity(byBitMask)) // only one bit set (parity odd)
break; // -> valid H(i) mask
}
wMi >>= 1; // next M(i) bit
i++; // next H(i) mask
}
// correct bit with H(i) mask
wMissed ^= byBitMask; // clear this missed bit
// parity odd -> wrong data value
if (Parity((BYTE) ((wData & byEmask[i]) ^ ((wData & wMi) >> 8))))
wData ^= byBitMask; // correct value
}
return wData & 0xFF; // only data byte is correct
}
VOID IrPrinter(BYTE c)
{
static INT nFrame = 0; // frame counter
static DWORD dwTiming[8]; // timing table
static WORD wData; // data container
static WORD wMissed; // missed bit container
static DWORD dwHalfBitTime; // current half bit time
static DWORD dwStartCyc; // cycle counter at start to detect overrrun
static DWORD dwLastValidCyc; // prior last timer value (fall back sync point)
static DWORD dwLastCyc; // last timer value at begin of last bit
BOOL bOverrun = FALSE; // overrun flag
DWORD dwCycles,dwDiffCycles;
INT i;
if (c == 0) return; // falling edge, ignore
_ASSERT(nFrame >= 0); // only positive framing values
// actual timestamp
dwCycles = (DWORD) (Chipset.cycles & 0xFFFFFFFF);
dwDiffCycles = dwCycles - dwLastCyc; // time difference from syncpoint
#if defined DEBUG_REDEYE_CYCLES
{
TCHAR buffer[256];
wsprintf(buffer,_T("Frame %d: Diff %u\n"),nFrame,dwDiffCycles);
OutputDebugString(buffer);
}
#endif
// rising edge
switch (nFrame)
{
case 0: // init
// workaraound for HP28S because inaccurate TIMER2 implementation
// may cause a timer interrupt too early
Chipset.inte = FALSE; // disable interrupt
dwStartCyc = dwLastValidCyc = dwLastCyc = dwCycles;
break;
case 1: // 2nd sync
// CPU cycles at SPEED setting
// 0 1 ... 4 5 6 7 8 ... 15 SPEED
// 1836 1857 4015 321 370 425 481 840 Cycles
if (dwDiffCycles > 1500) // the difference is too big
{
nFrame = 0; // reset, handle as 1st frame
dwStartCyc = dwLastValidCyc = dwLastCyc = dwCycles;
}
break;
case 2: // 3rd sync
// get difference between (dwCycles - dwLastCyc) and (dwLastCyc - dwLastValidCyc)
_ASSERT (dwLastCyc - dwLastValidCyc == dwDiffCycles);
// median half bit time
dwHalfBitTime = (dwCycles - dwLastValidCyc) / 2;
#if 0
{
TCHAR buffer[256];
wsprintf(buffer,"Halfbit Time = %u\n",dwHalfBitTime);
OutputDebugString(buffer);
}
#endif
// create timing table
for (i = 0; i < ARRAYSIZEOF(dwTiming); ++i)
{
dwTiming[i] = dwHalfBitTime * i;
dwTiming[i] += dwHalfBitTime * MAX(4,i) / 8;
}
dwCycles = dwLastCyc; // set syncpoint
wData = wMissed = 0; // preset data register
break;
default: // 12 data frames
// overrun condition
if ((dwCycles - dwStartCyc) > (30 * dwHalfBitTime))
{
_ASSERT(FALSE); // should not happen with OS caller
bOverrun = TRUE; // overrun flag
for (;nFrame <= 14; ++nFrame) // frame not complete
{
wData <<= 1; // add 0 to data (missed)
wMissed = (wMissed << 1) | 1; // add 1 to missed bit
}
nFrame = 14; // last frame
break;
}
// get table entry form timing
for (i = 0; i < ARRAYSIZEOF(dwTiming); ++i)
{
if ((INT) (dwTiming[i] - dwDiffCycles) >= 0)
break;
}
switch (i) // eval timing information
{
case 0: // too short
return; // exit without changing cycle references
case 1: // 2nd burst in same bit
_ASSERT(nFrame > 3); // clear only data frames
do
{
--nFrame; // remove last frame
wData >>= 1; // remove last data bit
wMissed >>= 1; // remove corresponding missed bit
}
while ((wMissed & 0x1) != 0); // remove all missed bits
dwLastCyc = dwLastValidCyc; // restore to last valid syncpoint
return; // exit without changing cycle references
case 6: // XX1
++nFrame; // add 1 missed frame
wData <<= 1; // add 0 to data (missed)
wMissed = (wMissed << 1) | 1; // add 1 to missed bit
// no break
case 4: // X1
++nFrame; // add 1 missed frame
wData <<= 1; // add 0 to data (missed)
wMissed = (wMissed << 1) | 1; // add 1 to missed bit
// no break
case 2: // 1
wData = (wData << 1) | 1; // add 1 to data
wMissed <<= 1; // no missed bit
break;
case 7: // XX0
++nFrame; // add 1 missed frame
wData <<= 1; // add 0 to data (missed)
wMissed = (wMissed << 1) | 1; // add 1 to missed bit
// no break
case 5: // X0
++nFrame; // add 1 missed frame
wData <<= 1; // add 0 to data (missed)
wMissed = (wMissed << 1) | 1; // add 1 to missed bit
// no break
case 3: // 0
wData <<= 1; // add 0 to data
wMissed <<= 1; // no missed bit
dwCycles -= dwHalfBitTime;
break;
default: // too many
wMissed = 0xFFFF; // fill missed bit counter
break;
}
break;
}
if (nFrame == 14) // last frame received, decode data
{
WORD w;
INT nCount;
nFrame = 0; // reset for next character
#if defined DEBUG_REDEYE_DATA
{
TCHAR buffer[256];
wsprintf(buffer,_T("Frame %d: Data = %03X Miss = %03X "),nFrame,wData,wMissed);
OutputDebugString(buffer);
}
#endif
// count number of missed bits
for (nCount = 0, w = wMissed; w != 0; ++nCount)
w &= (w - 1);
if (nCount <= 2) // error can be fixed
{
BYTE byOrgParity,byNewParity;
byOrgParity = wData >> 8; // the original parity information with missed bits
byNewParity = ~(wMissed >> 8); // missed bit mask for recalculated parity
if (nCount > 0) // error correction
{
wData = CorrectData(wData,wMissed);
}
wData &= 0xFF; // remove parity information
// recalculate parity data
byNewParity &= CreateCorrectionBits((BYTE) wData);
// wrong parity
if (byOrgParity != byNewParity)
wData = ERR_CHAR; // character for transfer error
}
else
{
wData = ERR_CHAR; // character for transfer error
}
#if defined DEBUG_REDEYE_DATA
{
TCHAR buffer[256];
if (isprint(wData))
wsprintf(buffer,_T("-> '%c'\n"),wData);
else
wsprintf(buffer,_T("-> %02X\n"),wData);
OutputDebugString(buffer);
}
#endif
SendByteUdp((BYTE) wData); // send data byte
if (bOverrun) IrPrinter(c); // call for start frame
return;
}
++nFrame; // frame valid, next frame
dwLastValidCyc = dwLastCyc; // update sync points
dwLastCyc = dwCycles;
return;
}