-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtape.go
More file actions
306 lines (259 loc) · 7.85 KB
/
Copy pathtape.go
File metadata and controls
306 lines (259 loc) · 7.85 KB
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
305
306
package main
import (
"encoding/binary"
"fmt"
"strings"
)
// ============================================================================
// Complete Tick() Implementation
// ============================================================================
// Tick advances the tape by a number of CPU T-states and toggles EAR for accurate mode.
// In Fast mode, it tries the ROM fastloader (if any), then instant-injects CODE blocks.
func (t *Tape) Tick(cycles int) {
if t == nil || t.st == nil || !t.st.Playing || !t.st.Loaded {
return
}
if t.st.Mode == TapeAccurate {
// Advance through pulse stream
for cycles > 0 && t.st.Position < len(t.st.Pulses) {
remain := t.st.Pulses[t.st.Position] - t.st.EdgeOffset
if cycles < remain {
t.st.EdgeOffset += cycles
cycles = 0
break
}
// End of this pulse
cycles -= remain
t.st.Position++
t.st.EdgeOffset = 0
// Toggle EAR level each pulse end
t.st.EarLevel = !t.st.EarLevel
// Update the I/O port EAR bit
if t.zx != nil && t.zx.io != nil {
t.zx.io.tapeEar = t.st.EarLevel
}
}
if t.st.Position >= len(t.st.Pulses) {
// Stop at end
t.st.Playing = false
fmt.Println("Tape: End of tape reached")
}
return
}
// Fast mode - try ROM trap or do instant injection
if t.st.Mode == TapeFast {
// Option 1: Try ROM fastloader interception (if available)
if t.fl != nil && t.fl.Enabled {
if t.fl.TryIntercept(t.zx, t) {
// Successfully intercepted and loaded
t.st.Playing = false
return
}
}
// Option 2: Check if we're in ROM LOAD routine (addresses for 48K ROM)
// Common addresses: 0x0556 (LD-BYTES), 0x04C2 (SA-BYTES)
pc := t.zx.cpu.PC
if pc >= 0x0556 && pc <= 0x0605 {
// We're in the ROM loader area, inject next block
t.fastInjectNextBlock()
return
}
// Option 3: If just started playing, inject all CODE blocks immediately
if t.st.Position == 0 {
t.fastInjectAll()
t.st.Playing = false
}
}
}
// fastInjectNextBlock injects the next block at current position
func (t *Tape) fastInjectNextBlock() {
if t.st.Position >= len(t.st.Blocks) {
t.st.Playing = false
return
}
blk := t.st.Blocks[t.st.Position]
// If it's a header and there's a data block following, load both
if blk.IsHeader && t.st.Position+1 < len(t.st.Blocks) {
dataBlk := t.st.Blocks[t.st.Position+1]
if instantLoadHeaderAndData(t.zx, blk.Data, dataBlk.Data) {
fmt.Printf("Tape: Fast-loaded block %d (header+data)\n", t.st.Position)
t.st.Position += 2
} else {
t.st.Position++ // Skip failed block
}
} else {
// Try to load as standalone data block
if len(blk.Data) > 0 {
// For non-header blocks, try direct memory injection if it looks like CODE
// This is a simplified approach - you may want more sophisticated detection
t.st.Position++
}
}
if t.st.Position >= len(t.st.Blocks) {
t.st.Playing = false
fmt.Println("Tape: Fast load complete")
}
}
// fastInjectAll injects all CODE blocks immediately
func (t *Tape) fastInjectAll() {
loaded := 0
for i := 0; i < len(t.st.Blocks); i++ {
blk := t.st.Blocks[i]
if blk.IsHeader && i+1 < len(t.st.Blocks) {
dataBlk := t.st.Blocks[i+1]
if instantLoadHeaderAndData(t.zx, blk.Data, dataBlk.Data) {
loaded++
i++ // Skip data block since we processed it
}
}
}
if loaded > 0 {
fmt.Printf("Tape: Fast-loaded %d blocks\n", loaded)
}
t.st.Position = len(t.st.Blocks) // Mark as complete
}
// ============================================================================
// Status and Control Methods
// ============================================================================
// GetStatus returns human-readable tape status
func (t *Tape) GetStatus() string {
if t == nil || t.st == nil || !t.st.Loaded {
return "No tape loaded"
}
mode := "Accurate"
if t.st.Mode == TapeFast {
mode = "Fast"
}
status := "Stopped"
if t.st.Playing {
status = "Playing"
}
progress := 0
if t.st.Mode == TapeAccurate && len(t.st.Pulses) > 0 {
progress = (t.st.Position * 100) / len(t.st.Pulses)
} else if len(t.st.Blocks) > 0 {
progress = (t.st.Position * 100) / len(t.st.Blocks)
}
return fmt.Sprintf("%s [%s, %s, %d%%]",
getFilenameOnly(t.st.Filename), mode, status, progress)
}
// GetBlockInfo returns information about tape blocks
func (t *Tape) GetBlockInfo() []string {
if t == nil || t.st == nil || !t.st.Loaded {
return nil
}
var info []string
for i, blk := range t.st.Blocks {
marker := " "
if t.st.Playing && i == t.st.Position {
marker = "> "
}
if blk.IsHeader && len(blk.Data) == 19 {
// Decode header
blockType := blk.Data[1]
filename := string(blk.Data[2:12])
length := binary.LittleEndian.Uint16(blk.Data[12:14])
typeStr := "Unknown"
switch blockType {
case 0:
typeStr = "Program"
case 1:
typeStr = "Num Array"
case 2:
typeStr = "Char Array"
case 3:
typeStr = "Code"
}
info = append(info, fmt.Sprintf("%s%d: Header '%s' %s %d bytes",
marker, i, strings.TrimRight(filename, " "), typeStr, length))
} else {
info = append(info, fmt.Sprintf("%s%d: Data %d bytes",
marker, i, len(blk.Data)))
}
}
return info
}
// ============================================================================
// Fast Loader Hook System
// ============================================================================
type FastLoader struct {
Enabled bool
}
// TryIntercept attempts to intercept ROM loading routine and inject data directly
func (fl *FastLoader) TryIntercept(zx *ZenZX, t *Tape) bool {
if !fl.Enabled || zx == nil || t == nil {
return false
}
// Check if we're at a ROM loading entry point
pc := zx.cpu.PC
// 48K ROM LOAD-BYTES routine entry points
if pc == 0x0556 || pc == 0x053F {
// The ROM expects:
// IX = start address
// DE = length
// A = flag byte (0x00 for header, 0xFF for data)
// Carry flag set means LOAD, clear means VERIFY
// For now, just do a simple fast load of next block
if t.st.Position < len(t.st.Blocks) {
t.fastInjectNextBlock()
// Set up return conditions as if load succeeded
// Set carry flag (bit 0 of F register) to indicate success
zx.cpu.F |= 0x01
// Return from the ROM routine
// Pop the return address and jump there
low := zx.memory.Read(zx.cpu.SP)
high := zx.memory.Read(zx.cpu.SP + 1)
zx.cpu.SP += 2
zx.cpu.PC = uint16(high)<<8 | uint16(low)
return true
}
}
return false
}
// ============================================================================
// Helper Functions
// ============================================================================
// instantLoadHeaderAndData emulates the ROM loader result for CODE blocks.
// Returns true if successfully loaded.
func instantLoadHeaderAndData(zx *ZenZX, header, data []byte) bool {
if len(header) != 19 || header[0] != 0x00 {
return false
}
typeByte := header[1]
length := binary.LittleEndian.Uint16(header[12:14])
start := binary.LittleEndian.Uint16(header[14:16])
// Only handle CODE blocks (type 3) for now
if typeByte != 3 {
return false
}
// Data block usually starts with flag byte (0xFF for data)
payload := data
if len(payload) > 0 && (payload[0] == 0xFF || payload[0] == 0x00) {
payload = payload[1:] // Skip flag byte
}
// Ensure we have enough data (minus checksum byte)
if len(payload) < int(length) {
return false
}
// Load data into memory
for i := 0; i < int(length); i++ {
zx.memory.Write(start+uint16(i), payload[i])
}
// Flash border to indicate load (like ROM does)
if zx.io != nil {
zx.io.borderColor = (zx.io.borderColor + 1) & 0x07
}
return true
}
// hasSuffixFold checks if filename has suffix (case-insensitive)
func hasSuffixFold(s, suffix string) bool {
return strings.HasSuffix(strings.ToLower(s), strings.ToLower(suffix))
}
// getFilenameOnly returns just the filename from a path
func getFilenameOnly(path string) string {
parts := strings.Split(path, "/")
if len(parts) > 0 {
return parts[len(parts)-1]
}
return path
}