-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw.h
78 lines (64 loc) · 1.55 KB
/
hw.h
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
/*
* Copyright (c) [2016], [Curt Meyers]
* All rights reserved.
*/
#pragma once
#include <sys/io.h>
#include <unistd.h>
#include <stdint.h>
class HW {
public:
HW(unsigned base) : sim(false), BASE(base), STATUS(base+1), CONTROL(base+2) { };
int init();
inline void portIn() {
ctl |= (DIRECTION | SELECT);
if (!sim) {
outb(ctl, CONTROL);
}
}
inline void portOut() {
ctl &= ~(DIRECTION | SELECT);
if (!sim) {
outb(ctl, CONTROL);
}
}
void control(uint8_t reg); // write to a control register
void writeData(uint8_t data, long nsDelay = 0); // latch data
void writeDataOnly(uint8_t data) { outb(data, BASE); }
void readData(uint8_t *result);
void setInit(bool high) {
if (high) {
outb((inb(CONTROL) | INIT), CONTROL);
}
else {
outb((inb(CONTROL) & ~INIT), CONTROL);
}
}
void setStrobe(bool high) {
if (high) {
outb((inb(CONTROL) & ~STROBE), CONTROL);
}
else {
outb((inb(CONTROL) | STROBE), CONTROL); // stobe low - hardware inverted
}
}
void setSelect(bool high) {
if (high) {
outb((inb(CONTROL) & ~SELECT), CONTROL);
}
else {
outb((inb(CONTROL) | SELECT), CONTROL); // select low - hardware inverted
}
}
private:
bool sim;
uint8_t ctl;
const unsigned BASE;
const unsigned STATUS;
const unsigned CONTROL;
// CONTROL register
static const unsigned STROBE = 0x1; //Inverted
static const unsigned INIT = 0x4;
static const unsigned SELECT = 0x8; //Inverted
static const unsigned DIRECTION = 0x20;
};