-
Notifications
You must be signed in to change notification settings - Fork 17
/
tape.go
271 lines (228 loc) · 5.83 KB
/
tape.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
package spectrum
import (
"github.com/remogatto/gospeccy/src/formats"
"io/ioutil"
"sync"
)
const (
TAPE_DRIVE_START = iota
TAPE_DRIVE_STOP
TAPE_DRIVE_PAUSE
TAPE_DRIVE_LEADER
TAPE_DRIVE_SYNC
TAPE_DRIVE_NEWBIT
TAPE_DRIVE_HALF2
TAPE_DRIVE_PAUSE_STOP
TAPE_DRIVE_NEWBYTE
)
const (
TAPE_LEADER = 2168
TAPE_FIRST_SYNC = 667
TAPE_SECOND_SYNC = 735
TAPE_SET_BIT = 1710
TAPE_UNSET_BIT = 855
TAPE_HEADER_LEADER_PULSES = 8063
TAPE_DATA_LEADER_PULSES = 3223
TAPE_PAUSE = 3500000
)
const TAPE_ACCELERATION_IN_FPS = DefaultFPS * 20
type Tape struct {
tap *formats.TAP
}
func NewTape(tap *formats.TAP) *Tape {
return &Tape{tap}
}
func NewTapeFromFile(filename string) (*Tape, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
tap, err := formats.NewTAP(data)
if err != nil {
return nil, err
}
return &Tape{tap}, nil
}
func (tape *Tape) At(pos uint) byte {
return tape.tap.At(pos)
}
type TapeDrive struct {
AcceleratedLoad bool
NotifyLoadComplete bool
speccy *Spectrum48k
tape *Tape
pos uint
tstate, lastIn uint64
earBit byte
timeout int
timeLastIn, currBlockLen, currBlockId int
leaderPulses, bitTime uint16
state, mask byte
accelerating bool
fpsBeforeAcceleration float32
notifyCpuLoadCompleted bool
loadComplete chan bool
mutex sync.RWMutex
}
func NewTapeDrive() *TapeDrive {
return &TapeDrive{
pos: 0,
earBit: 0xbf,
loadComplete: make(chan bool),
}
}
func (tapeDrive *TapeDrive) init(speccy *Spectrum48k) {
tapeDrive.speccy = speccy
}
func (tapeDrive *TapeDrive) Insert(tape *Tape) {
tapeDrive.tape = tape
}
func (tapeDrive *TapeDrive) Play() {
tapeDrive.speccy.readFromTape = true
tapeDrive.pos = 0
tapeDrive.state = TAPE_DRIVE_START
tapeDrive.timeout = 0
tapeDrive.timeLastIn = 0
}
func (tapeDrive *TapeDrive) Stop() {
tapeDrive.speccy.readFromTape = false
tapeDrive.pos = 0
tapeDrive.state = TAPE_DRIVE_PAUSE_STOP
tapeDrive.timeout = 0
tapeDrive.timeLastIn = 0
tapeDrive.currBlockId = 0
}
func (tapeDrive *TapeDrive) accelerate() {
if !tapeDrive.accelerating {
tapeDrive.accelerating = true
go func() {
oldFPS_chan := make(chan float32)
tapeDrive.speccy.CommandChannel <- Cmd_SetFPS{TAPE_ACCELERATION_IN_FPS, oldFPS_chan}
oldFPS := <-oldFPS_chan
tapeDrive.mutex.Lock()
tapeDrive.fpsBeforeAcceleration = oldFPS
tapeDrive.mutex.Unlock()
}()
}
}
func (tapeDrive *TapeDrive) decelerate() {
if tapeDrive.accelerating {
tapeDrive.accelerating = false
go func() {
tapeDrive.mutex.RLock()
fps := tapeDrive.fpsBeforeAcceleration
tapeDrive.mutex.RUnlock()
tapeDrive.speccy.CommandChannel <- Cmd_SetFPS{fps, nil}
}()
}
}
func (tapeDrive *TapeDrive) doPlay() (endOfBlock bool) {
now := int(tapeDrive.speccy.ula.frame)*TStatesPerFrame + tapeDrive.speccy.Cpu.Tstates
tapeDrive.timeout -= now - tapeDrive.timeLastIn
tapeDrive.timeLastIn = now
if tapeDrive.timeout > 0 {
return
}
tapeDrive.timeout = 0
if tapeDrive.AcceleratedLoad {
tapeDrive.accelerate()
} else {
tapeDrive.decelerate()
}
switch tapeDrive.state {
case TAPE_DRIVE_START:
currBlock := tapeDrive.tape.tap.GetBlock(tapeDrive.currBlockId)
tapeDrive.currBlockLen = currBlock.Len()
if currBlock.BlockType() == formats.TAP_BLOCK_HEADER {
tapeDrive.leaderPulses = TAPE_HEADER_LEADER_PULSES
} else {
tapeDrive.leaderPulses = TAPE_DATA_LEADER_PULSES
}
tapeDrive.earBit = 0xbf
tapeDrive.timeout = TAPE_LEADER
tapeDrive.state = TAPE_DRIVE_LEADER
case TAPE_DRIVE_LEADER:
if tapeDrive.earBit == 0xbf {
tapeDrive.earBit = 0xff
} else {
tapeDrive.earBit = 0xbf
}
tapeDrive.leaderPulses--
if tapeDrive.leaderPulses > 0 {
tapeDrive.timeout = TAPE_LEADER
} else {
tapeDrive.timeout = TAPE_FIRST_SYNC
tapeDrive.state = TAPE_DRIVE_SYNC
}
case TAPE_DRIVE_SYNC:
if tapeDrive.earBit == 0xbf {
tapeDrive.earBit = 0xff
} else {
tapeDrive.earBit = 0xbf
}
tapeDrive.timeout = TAPE_SECOND_SYNC
tapeDrive.state = TAPE_DRIVE_NEWBYTE
case TAPE_DRIVE_NEWBYTE:
tapeDrive.mask = 0x80
fallthrough
case TAPE_DRIVE_NEWBIT:
if tapeDrive.earBit == 0xbf {
tapeDrive.earBit = 0xff
} else {
tapeDrive.earBit = 0xbf
}
if (tapeDrive.tape.At(tapeDrive.pos) & tapeDrive.mask) == 0 {
tapeDrive.bitTime = TAPE_UNSET_BIT
} else {
tapeDrive.bitTime = TAPE_SET_BIT
}
tapeDrive.timeout = int(tapeDrive.bitTime)
tapeDrive.state = TAPE_DRIVE_HALF2
case TAPE_DRIVE_HALF2:
if tapeDrive.earBit == 0xbf {
tapeDrive.earBit = 0xff
} else {
tapeDrive.earBit = 0xbf
}
tapeDrive.timeout = int(tapeDrive.bitTime)
tapeDrive.mask >>= 1
if tapeDrive.mask == 0 {
tapeDrive.pos++
tapeDrive.currBlockLen--
if tapeDrive.currBlockLen > 0 {
tapeDrive.state = TAPE_DRIVE_NEWBYTE
} else {
tapeDrive.state = TAPE_DRIVE_PAUSE
}
} else {
tapeDrive.state = TAPE_DRIVE_NEWBIT
}
case TAPE_DRIVE_PAUSE:
if tapeDrive.earBit == 0xbf {
tapeDrive.earBit = 0xff
} else {
tapeDrive.earBit = 0xbf
}
endOfBlock = true
tapeDrive.decelerate()
if tapeDrive.pos < tapeDrive.tape.tap.Len() {
tapeDrive.timeout = TAPE_PAUSE
tapeDrive.state = TAPE_DRIVE_PAUSE_STOP
} else {
tapeDrive.timeout = 1
tapeDrive.state = TAPE_DRIVE_STOP
tapeDrive.speccy.readFromTape = false
tapeDrive.notifyCpuLoadCompleted = true
}
case TAPE_DRIVE_PAUSE_STOP:
tapeDrive.currBlockId++
tapeDrive.state = TAPE_DRIVE_START
}
return endOfBlock
}
func (tapeDrive *TapeDrive) getEarBit() uint8 {
return tapeDrive.earBit
}
func (tapeDrive *TapeDrive) LoadComplete() <-chan bool {
return tapeDrive.loadComplete
}