-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.go
182 lines (162 loc) · 3.06 KB
/
uart.go
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"sync"
)
const (
ierRxintBit = 0x1
ierThreintBit = 0x2
iirThrEmpty = 0x2
iirRdAvailable = 0x4
iirNoInterrupt = 0x7
lsrDataAvailable = 0x1
lsrThrEmpty = 0x20
)
type Uart struct {
clock uint64
rbr uint8 // receiver buffer register
thr uint8 // transmitter holding register
ier uint8 // interrupt enable register
iir uint8 // interrupt identification register
lcr uint8 // line control register
mcr uint8 // modem control register
lsr uint8 // line status register
scr uint8 // scratch,
threip bool
interrupting bool
sync.Mutex
buffer []byte
}
func NewUart() *Uart {
u := &Uart{
clock: 0,
rbr: 0,
thr: 0,
ier: 0,
iir: 0,
lcr: 0,
mcr: 0,
lsr: lsrThrEmpty,
scr: 0,
threip: false,
interrupting: false,
buffer: []byte{}, // stdin buffer
}
// read input
go func() {
r := bufio.NewReader(os.Stdin)
for {
b, err := r.ReadByte()
if err != nil {
if err == io.EOF {
continue
}
fmt.Fprintf(os.Stderr, "read stdin: %s", err)
continue
}
u.Lock()
u.buffer = append(u.buffer, b)
u.Unlock()
}
}()
return u
}
func (u *Uart) Tick() {
u.clock++
rxip := false
// read input and store into register
if u.clock%0x38400 == 0 && u.rbr == 0 {
// get single byte
u.Lock()
b := u.buffer[0]
u.buffer = u.buffer[1:]
u.Unlock()
if b != 0 {
u.rbr = b
u.lsr |= lsrDataAvailable
u.updateIir()
if u.ier&ierRxintBit != 0 {
rxip = true
}
}
}
// write reg value to stdout
if u.clock%0x10 == 0 && u.thr != 0 {
fmt.Fprint(os.Stdout, string(u.thr))
u.thr = 0
u.lsr |= lsrThrEmpty
u.updateIir()
if u.ier&ierThreintBit != 0 {
u.threip = true
}
}
if u.threip || rxip {
u.interrupting = true
u.threip = false
} else {
u.interrupting = false
}
}
func (u *Uart) updateIir() {
rxip := u.ier&ierRxintBit != 0 && u.rbr != 0
threip := u.ier&ierThreintBit != 0 && u.thr == 0
if rxip {
u.iir = iirRdAvailable
} else if threip {
u.iir = iirThrEmpty
} else {
u.iir = iirNoInterrupt
}
}
func (u *Uart) read(address uint64) uint8 {
switch address {
case 0x10000000:
if (u.lcr >> 7) == 0 {
rbr := u.rbr
u.rbr = 0
u.lsr &= ^uint8(lsrDataAvailable)
u.updateIir()
return rbr
}
case 0x10000001:
if (u.lcr >> 7) == 0 {
return u.ier
}
case 0x10000002:
return u.iir
case 0x10000003:
return u.lcr
case 0x10000004:
return u.mcr
case 0x10000005:
return u.lsr
case 0x10000007:
return u.scr
}
return 0
}
func (u *Uart) write(address uint64, value uint8) {
switch address {
case 0x10000000:
if (u.lcr >> 7) == 0 {
u.thr = value
u.lsr &= ^uint8(lsrThrEmpty)
u.updateIir()
}
case 0x10000001:
if u.ier&ierThreintBit == 0 && value&ierThreintBit != 0 && u.thr == 0 {
u.threip = true
}
u.ier = value
u.updateIir()
case 0x10000003:
u.lcr = value
case 0x10000004:
u.mcr = value
case 0x10000007:
u.scr = value
}
}