-
Notifications
You must be signed in to change notification settings - Fork 8
/
rtc.go
304 lines (266 loc) · 6.06 KB
/
rtc.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
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
package main
import (
"ndsemu/emu/hwio"
log "ndsemu/emu/logger"
"time"
)
var modRtc = log.NewModule("rtc")
type SerialDevice interface {
ReadData() uint8
WriteData(val uint8)
}
type HwSerial3W struct {
cs bool
clk bool
datadir bool
data uint8
cnt int
dev SerialDevice
Serial hwio.Reg8 `hwio:"rcb,wcb"`
}
func (s *HwSerial3W) WriteSERIAL(_, val uint8) {
cs := val&(1<<2) != 0
clk := val&(1<<1) != 0
datadir := val&(1<<4) != 0
// log.Infof("cs=%v clk=%v datadir=%v data=%d", cs, clk, datadir, val&1)
// select rising edge: begin new transfer
// select and change direction: begin new transfer
if cs && !s.cs {
s.data = 0
if datadir {
s.cnt = 0
} else {
s.cnt = 8
}
}
// select=1 -> active
// falling edge -> transfer
if cs && !clk && s.clk {
if datadir {
// WRITE
s.data >>= 1
s.data |= (val & 1) << 7
s.cnt++
if s.cnt == 8 {
// log.Infof("writing byte: %x", s.data)
s.dev.WriteData(s.data)
s.cnt = 0
}
} else {
// READ
s.data >>= 1
s.cnt++
if s.cnt == 8 || s.datadir {
s.data = s.dev.ReadData()
s.cnt = 0
// log.Infof("begin next byte(2): %x (first bit: %d)", s.data, s.data&1)
} else {
// log.Infof("prepare next bit: idx=%d, val=%d", s.cnt, s.data&1)
}
}
}
s.cs = cs
s.clk = clk
s.datadir = datadir
}
func (s *HwSerial3W) ReadSERIAL(_ uint8) uint8 {
val := uint8(0x60)
if s.clk {
val |= (1 << 1)
}
if s.cs {
val |= (1 << 2)
}
if s.datadir {
val |= (1 << 4)
}
val |= (s.data & 1)
// log.Infof("reading bit: %d", s.data&1)
return val
}
// Seiko S-35180
type HwRtc struct {
HwSerial3W
regStatus1 uint8
regStatus2 uint8
writing bool
buf []byte
idx int
alarms [2]struct {
dow byte
hour byte
minOrFreq byte
}
}
func NewHwRtc() *HwRtc {
rtc := new(HwRtc)
rtc.regStatus1 = 0x00 // 0x80: reset to defaults
rtc.regStatus2 = 0x00
rtc.HwSerial3W.dev = rtc
hwio.MustInitRegs(&rtc.HwSerial3W)
return rtc
}
func (rtc *HwRtc) ResetDefaults() {
rtc.regStatus1 = 0x80
rtc.regStatus2 = 0x00
}
func (rtc *HwRtc) ReadData() uint8 {
if rtc.writing {
modRtc.WarnZ("read during register writing").End()
return 0
}
if rtc.idx >= len(rtc.buf) {
modRtc.WarnZ("read but not data setup").End()
return 0
}
data := rtc.buf[rtc.idx]
rtc.idx++
return data
}
func (rtc *HwRtc) bcd(value uint) uint8 {
if value > 99 {
modRtc.WarnZ("cannot convert value to BCD").Uint("value", value).End()
return 0xFF
}
value = (value/10)*16 + (value % 10)
return uint8(value)
}
func (rtc *HwRtc) alarm1HasFreq() bool {
return rtc.regStatus2&(1<<2) == 0
}
const (
RtcRegSr1 = iota
RtcRegAlarm1
RtcRegDatetime
RtcRegClockAdjust
RtcRegSr2
RtcRegAlarm2
RtcRegTime
RtcRegUnused
)
var rtcRegnames = [8]string{"sr1", "alarm1", "datetime", "clockadjust", "sr2", "alarm2", "time", "unused"}
func (rtc *HwRtc) writeReg(val uint8) {
reglen := [8]int{1, 3, 7, 1, 1, 3, 3, 1}
// Configuration of alarm1 depends on statusReg2. In some modes, there is just one
// parameter (the frequency)
if rtc.alarm1HasFreq() {
reglen[1] = 1
}
rtc.buf = append(rtc.buf, val)
if len(rtc.buf) != reglen[rtc.idx] {
modRtc.Infof("partial writing reg %q: %02x", rtcRegnames[rtc.idx], val)
return
}
rtc.writing = false
modRtc.Infof("final writing reg %q: %02x", rtcRegnames[rtc.idx], val)
switch rtc.idx {
case RtcRegSr1:
rtc.regStatus1 = (rtc.regStatus1 & 0xF0) | (val & 0xE)
modRtc.Infof("write sr1: %02x", val)
case RtcRegSr2:
rtc.regStatus2 = val
modRtc.Infof("write sr2: %02x", val)
case RtcRegAlarm1:
if len(rtc.buf) == 1 {
rtc.alarms[0].minOrFreq = rtc.buf[0]
if rtc.buf[0] != 0 {
modRtc.Errorf("alarm1 set but not implemented: %x", rtc.buf)
}
} else {
rtc.alarms[0].dow = rtc.buf[0]
rtc.alarms[0].hour = rtc.buf[1]
rtc.alarms[0].minOrFreq = rtc.buf[2]
if rtc.buf[0] != 0 || rtc.buf[1] != 0 || rtc.buf[2] != 0 {
modRtc.Errorf("alarm1 set but not implemented: %x", rtc.buf)
}
}
case RtcRegAlarm2:
rtc.alarms[1].dow = rtc.buf[0]
rtc.alarms[1].hour = rtc.buf[1]
rtc.alarms[1].minOrFreq = rtc.buf[2]
if rtc.buf[0] != 0 || rtc.buf[1] != 0 || rtc.buf[2] != 0 {
modRtc.Errorf("alarm2 set but not implemented: %x", rtc.buf)
}
default:
modRtc.Warnf("unimplemented register write: %q=%x", rtcRegnames[rtc.idx], rtc.buf)
}
}
func (rtc *HwRtc) WriteData(val uint8) {
if rtc.writing {
rtc.writeReg(val)
return
}
if val&0xF != 6 {
modRtc.Warnf("invalid command %02x", val)
return
}
read := val&0x80 != 0
reg := (val >> 4) & 7
if !read {
modRtc.Infof("begin writing reg %q", rtcRegnames[reg])
rtc.writing = true
rtc.buf = nil
rtc.idx = int(reg)
return
}
rtc.buf = nil
rtc.idx = 0
switch reg {
case RtcRegSr1:
rtc.buf = append(rtc.buf, rtc.regStatus1)
// Bit 4-7 are auto-cleared after read
// (though currently we don't set them in our emulation...)
rtc.regStatus1 &= 0x0F
case RtcRegSr2:
rtc.buf = append(rtc.buf, rtc.regStatus2)
case RtcRegDatetime, RtcRegTime:
now := time.Now()
var hour uint8
if rtc.regStatus1&2 != 0 {
// 24H mode
hour = rtc.bcd(uint(now.Hour()))
} else {
// 12H mode, with 12:00 that becomes 0pm instead of 12pm (as per
// normale human convention)
hour = rtc.bcd(uint(now.Hour() % 12))
if now.Hour() >= 12 {
hour |= 0x40
}
}
if reg == 2 { // datetime contains also the date
rtc.buf = append(rtc.buf,
rtc.bcd(uint(now.Year()-2000)),
rtc.bcd(uint(now.Month())),
rtc.bcd(uint(now.Day())),
rtc.bcd(uint(now.Weekday())),
)
}
rtc.buf = append(rtc.buf,
hour,
rtc.bcd(uint(now.Minute())),
rtc.bcd(uint(now.Second())),
)
case RtcRegAlarm1:
if rtc.alarm1HasFreq() {
rtc.buf = append(rtc.buf,
rtc.alarms[0].minOrFreq,
)
} else {
rtc.buf = append(rtc.buf,
rtc.alarms[0].dow,
rtc.alarms[0].hour,
rtc.alarms[0].minOrFreq,
)
}
case RtcRegAlarm2:
rtc.buf = append(rtc.buf,
rtc.alarms[1].dow,
rtc.alarms[1].hour,
rtc.alarms[1].minOrFreq,
)
default:
modRtc.Warnf("unimplemented register read %q", rtcRegnames[reg])
return
}
modRtc.Infof("read %q: %x", rtcRegnames[reg], rtc.buf)
}