-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzxtape.cpp
More file actions
124 lines (114 loc) · 2.22 KB
/
zxtape.cpp
File metadata and controls
124 lines (114 loc) · 2.22 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
/*
* zxtape.cpp
*
* Created on: 25 mar 2016
* Author: wpk
*/
#include <fstream>
#include <iostream>
#include "zxtape.h"
using namespace std;
zxtape::zxtape(string filename) {
ifstream file;
file.open(filename.c_str(), std::ios::in | std::ios::binary);
if (file.fail()) {
throw ifstream::failure("Can't open TAP file");
}
file.seekg(0, std::ios_base::end);
len = file.tellg();
file.seekg(0);
buf = new char[len];
file.read(buf, len);
file.close();
reset();
}
void zxtape::newblock() {
pos = 0;
i_pos = 0;
blockoffs += blocklen;
if (blockoffs >= len) {
state = END;
} else {
blockstate = PILOT;
blocklen = * ( (uint16_t*) &buf[blockoffs]);
blockoffs += 2;
}
}
void zxtape::reset() {
state = PAUSE;
blockno = 0;
blockoffs = 0;
blocklen = 0;
newblock();
earr = false;
}
void zxtape::go() {
state = RUNNING;
}
bool zxtape::bit() {
return buf[blockoffs + (pos/8)] & (1 << (7 - (pos%8)));
}
void zxtape::flip() {
earr = !earr;
}
void zxtape::update_ticks(uint32_t diff) {
if (state != RUNNING) {
return;
}
// A 'pulse' here is either a mark or a space, so 2 pulses makes a complete square wave cycle.
// Pilot tone: before each block is a sequence of 8063 (header) or 3223 (data) pulses, each of length 2168 T-states.
// Sync pulses: the pilot tone is followed by two sync pulses of 667 and 735 T-states resp.
// A '0' bit is encoded as 2 pulses of 855 T-states each.
// A '1' bit is encoded as 2 pulses of 1710 T-states each (ie. twice the length of a '0')
i_pos += diff;
switch (blockstate) {
case PILOT:
if (i_pos > 2168) {
i_pos = 0;
flip();
if (++pos > 8000) {
blockstate = SYNC;
i_pos = 0;
pos = 0;
}
}
break;
case SYNC:
if (pos == 0) {
if (i_pos > 667) {
pos = 1;
i_pos = 0;
flip();
}
} else {
if (i_pos > 735) {
blockstate = DATA;
tock = false;
pos = 0;
i_pos = 0;
flip();
}
}
break;
case DATA: {
uint32_t l = bit() ? 1705 : 850; // 1710 : 855;
if (i_pos > 2*l) {
flip();
tock = false; // tick
i_pos = 0;
if (++pos == 8*blocklen) {
newblock();
}
} else if (i_pos > l && !tock) {
flip();
tock = true;
}
}
}
}
bool const zxtape::ear() {
return earr;
}
zxtape::~zxtape() {
delete[] buf;
}