1 | #ifndef _DEVICE_H_
|
---|
2 | #define _DEVICE_H_
|
---|
3 |
|
---|
4 | #pragma once
|
---|
5 |
|
---|
6 | #include <errno.h>
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <stdint.h>
|
---|
9 | #include <unistd.h>
|
---|
10 |
|
---|
11 | // FreeBSD
|
---|
12 | #include <sys/types.h>
|
---|
13 | #include <machine/cpufunc.h>
|
---|
14 | #include <machine/sysarch.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | class Device {
|
---|
18 | public:
|
---|
19 | Device()
|
---|
20 | {};
|
---|
21 |
|
---|
22 | virtual ~Device() {
|
---|
23 | shutdown();
|
---|
24 | };
|
---|
25 |
|
---|
26 | void shutdown();
|
---|
27 | virtual void power() = 0;
|
---|
28 |
|
---|
29 | virtual void read(const char* file) = 0;
|
---|
30 | virtual void write(const char* file) = 0;
|
---|
31 | virtual void erase(void) = 0;
|
---|
32 |
|
---|
33 | int setup(const int port_base) {
|
---|
34 | port = port_base;
|
---|
35 | if (i386_set_ioperm(port_base, 3, true) != 0)
|
---|
36 | {
|
---|
37 | perror("Unable to get access to parallel port");
|
---|
38 | return errno;
|
---|
39 | }
|
---|
40 | shutdown();
|
---|
41 |
|
---|
42 | return 0;
|
---|
43 | }
|
---|
44 | protected:
|
---|
45 | typedef enum {
|
---|
46 | NONE = 0b0011, /* Nothing selected */
|
---|
47 | NYBLE = 0b0100, /* Read low nyble */
|
---|
48 | /* When 0, status reg shows D0.3
|
---|
49 | * When 1, status reg shows D4.7
|
---|
50 | */
|
---|
51 | CTRL = 0b0101, /* Control reg. */
|
---|
52 | /* b0: A16
|
---|
53 | * b1: /A17 (A17 is reversed !)
|
---|
54 | * b2: WE
|
---|
55 | * b3: A18
|
---|
56 | * b4: OE
|
---|
57 | * b5: VPP on pin 25 (0 = enable)
|
---|
58 | * b6: VPP on pin 24 (0 = enable)
|
---|
59 | * b7: CE
|
---|
60 | */
|
---|
61 | VOLT = 0b0110, /* Power config reg. */
|
---|
62 | /* b0: VPP on pin 1 (0 = enable)
|
---|
63 | * b1: VPP on pin 3 (0 = enable)
|
---|
64 | * b2: VCC on pin 32 (0 = enable)
|
---|
65 | * b3: Data reg. output enable (and green led)
|
---|
66 | * b4: VPP config
|
---|
67 | * b5: VPP config
|
---|
68 | * 00 = 24V
|
---|
69 | * 01 = ??
|
---|
70 | * 10 = ??
|
---|
71 | * 11 = 12V
|
---|
72 | * b6: Address reg. output enable
|
---|
73 | * b7: VCC enable (1 = power ON)
|
---|
74 | */
|
---|
75 | ADR0 = 0b1101, /* Low address byte A0.7 */
|
---|
76 | DATA = 0b1110, /* Data byte */
|
---|
77 | ADR1 = 0b1111, /* High address byte A8.15 */
|
---|
78 | /* A13 is reversed ! (also used as VCC pin in smaller packages)
|
---|
79 | */
|
---|
80 | } Register;
|
---|
81 |
|
---|
82 | static const uint8_t A13 = 1 << 5;
|
---|
83 |
|
---|
84 | typedef enum {
|
---|
85 | A16 = 1,
|
---|
86 | A17 = 2,
|
---|
87 | WE = 4,
|
---|
88 | A18 = 8,
|
---|
89 | OE = 16,
|
---|
90 | VPP25 = 32,
|
---|
91 | VPP24 = 64,
|
---|
92 | CE = 128
|
---|
93 | } CTRLbits;
|
---|
94 |
|
---|
95 | typedef enum {
|
---|
96 | VPP1 = 1,
|
---|
97 | VPP3 = 2,
|
---|
98 | VCC32 = 4,
|
---|
99 | DOE = 8,
|
---|
100 |
|
---|
101 | V24 = 0,
|
---|
102 | V12 = 48,
|
---|
103 |
|
---|
104 | AOE = 64,
|
---|
105 | VCC30 = 128
|
---|
106 | } VOLTbits;
|
---|
107 |
|
---|
108 | static inline void write(const Register reg, const uint8_t val)
|
---|
109 | {
|
---|
110 | // To avoid glitches, first set the bits that select the register,
|
---|
111 | // then enable the output. And disable the output when we are done
|
---|
112 | // while not touching the selected reg. This is the only way to
|
---|
113 | // avoid glitches (another reg may be selected long enough for
|
---|
114 | // losing some bits)
|
---|
115 | outb(port, val);
|
---|
116 | usleep(1000);
|
---|
117 | outb(port + 2, reg ^ 4);
|
---|
118 | usleep(1000);
|
---|
119 | outb(port + 2, reg);
|
---|
120 | usleep(1000);
|
---|
121 | outb(port + 2, reg ^ 4);
|
---|
122 | usleep(1000);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static inline uint8_t read()
|
---|
126 | {
|
---|
127 | outb(port+2, NYBLE ^ 4);
|
---|
128 | usleep(1000);
|
---|
129 | uint8_t byte = inb(port + 1) & 0xF0;
|
---|
130 |
|
---|
131 | outb(port+2, NYBLE);
|
---|
132 | usleep(1000);
|
---|
133 | byte |= inb(port + 1) >> 4;
|
---|
134 | outb(port+2, NYBLE ^ 4);
|
---|
135 | usleep(1000);
|
---|
136 | return byte ^ 0x88;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static int port;
|
---|
140 | private:
|
---|
141 | };
|
---|
142 |
|
---|
143 | #endif
|
---|